Design pattern composite 20120413 joncash 01

  • 300 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
300
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Implementing the composite pattern lets clients treat individual objects and compositions uniformly 組合模式 : 將物件組合成樹形結再以表示「部份 - 整體」的層次結構 組合模式使得用戶對單個物件和組合物件的使用具有一致性。
  • Composite can be used when clients should ignore the difference between compositions of objects and individual objects.

Transcript

  • 1. 1Design Pattern –CompositePresented by Joncash4/13/2012
  • 2. 2Outline• Definition• Motivation• When to Use• Structure• Application• Example
  • 3. 3Definition• The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object.• The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
  • 4. 4Motivation• When dealing with tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, error prone.
  • 5. 5When to Use• If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice
  • 6. 6Structure
  • 7. 7Classic Composite application• ASP.NET ▫ TreeView ▫ System.Web.UI.Control
  • 8. 8Examplestatic void Main() // Add and remove a leaf{ Leaf leaf = new Leaf("Leaf D"); // Create a tree structure root.Add(leaf); Composite root = new root.Remove(leaf); Composite("root"); root.Add(new Leaf("Leaf A")); // Recursively display tree root.Add(new Leaf("Leaf B")); root.Display(1); Composite comp = new // Wait for user Composite("Composite X"); Console.ReadKey(); comp.Add(new Leaf("Leaf XA")); comp.Add(new Leaf("Leaf XB")); } root.Add(comp); root.Add(new Leaf("Leaf C"));
  • 9. 9Componentabstract class Component{ protected string name; // Constructor public Component(string name) { this.name = name; } public abstract void Add(Component c); public abstract void Remove(Component c); public abstract void Display(int depth);}
  • 10. 10Compositeclass Composite : Component{ public override void Remove(Component com private List<Component> ponent) _children { = new List<Component>(); _children.Remove(component); } // Constructor public Composite(string name) public override void Display(int depth) : base(name) { { Console.WriteLine(new String(-, depth) + } name);public override void Add(Compon // Recursively display child nodes ent component) foreach (Component component in _children { ) _children.Add(component); { } component.Display(depth + 2); } }
  • 11. 11Leafclass Leaf : Component public override void Add(Component c){ { // Constructor Console.WriteLine("Cannot add to a public Leaf(string name) leaf"); : base(name) } { } public override void Remove(Component c) { Console.WriteLine("Cannot remove from a leaf"); } public override void Display(int depth) { Console.WriteLine(new String(-, depth) + name); }
  • 12. 12Reference• wiki/Composite_pattern• dofactory.com/Patterns/PatternComposite