1. 定义
将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
2. UML 类图
3. 结构代码
// Composite pattern -- Structural example
|
using System; using System.Collections.Generic;
namespace DoFactory.GangOfFour.Composite.Structural { /// /// MainApp startup class for Structural /// Composite Design Pattern. /// class { /// /// Entry point into console application. /// static { // Create a tree structure Composite root = root.Add(new root.Add(new
Composite comp = comp.Add(new comp.Add(new
root.Add(comp); root.Add(new
// Add and remove a leaf Leaf leaf = root.Add(leaf); root.Remove(leaf);
// Recursively display tree root.Display(1);
// Wait for user Console.ReadKey(); } }
/// /// The 'Component' abstract class /// abstract { protected
// Constructor public Component(string name) { this.name = name; }
public public public }
/// /// The 'Composite' class /// class { private
// Constructor public Composite(string name) : base(name) { }
public { _children.Add(component); }
public { _children.Remove(component); }
public { Console.WriteLine(new
// Recursively display child nodes foreach (Component component { component.Display(depth + 2); } } }
/// /// The 'Leaf' class /// class { // Constructor public Leaf(string name) : base(name) { }
public { Console.WriteLine("Cannot add to a leaf"); }
public { Console.WriteLine("Cannot remove from a leaf"); }
public { Console.WriteLine(new } } } |
Output
-root
---Leaf A ---Leaf B ---Composite X -----Leaf XA -----Leaf XB ---Leaf C |
4. 实例代码
// Composite pattern -- Real World example
|
using System; using System.Collections.Generic;
namespace DoFactory.GangOfFour.Composite.RealWorld { /// /// MainApp startup class for Real-World /// Composite Design Pattern. /// class { /// /// Entry point into console application. /// static { // Create a tree structure CompositeElement root = new root.Add(new root.Add(new root.Add(new
// Create a branch CompositeElement comp = new comp.Add(new comp.Add(new root.Add(comp);
// Add and remove a PrimitiveElement PrimitiveElement pe = new root.Add(pe); root.Remove(pe);
// Recursively display nodes root.Display(1);
// Wait for user Console.ReadKey(); } }
/// /// The 'Component' Treenode /// abstract { protected
// Constructor public DrawingElement(string name) { this._name = name; }
public public public }
/// /// The 'Leaf' class /// class { // Constructor public PrimitiveElement(string name) : base(name) { }
public { Console.WriteLine( "Cannot add to a PrimitiveElement"); }
public { Console.WriteLine( "Cannot remove from a PrimitiveElement"); }
public { Console.WriteLine( new } }
/// /// The 'Composite' class /// class { private new
// Constructor public CompositeElement(string name) : base(name) { }
public { elements.Add(d); }
public { elements.Remove(d); }
public { Console.WriteLine(new "+ " + _name);
// Display each child element on this node foreach (DrawingElement d { d.Display(indent + 2); } } } } |
Output
-+ Picture
--- Red Line --- Blue Circle --- Green Box ---+ Two Circles ----- Black Circle ----- White Circle |
该文章来自:http://www.dofactory.com/Patterns/PatternComposite.aspx