索引
将对象组合成树形结构以表示 “部分-整体” 的层次结构。
Composite 使得用户对于单个对象和组合对象的使用具有一致性。
Compose objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of objects uniformly.
典型的 Composite 对象结构:
Component
Leaf
Composite
Client
在以下情况下可以使用 Composite 模式:
Composite 模式的目的之一是使得用户不知道它们正在使用具体的 Leaf 和 Composite 类。
为达到这一目的,Component 需要为 Leaf 和 Composite 定义一些公共操作,并提供缺省的实现,而 Leaf 和 Composite 子类可以对它们进行重定义。
然而,这个目标会与类层次结构设计原则冲突,该原则规定:一个类只能定义那些对它的子类有意义的操作。
实现方式(一):在 Component 中定义公共接口以保持透明性但损失安全性。
在 Component 中定义 Add 和 Remove 操作需要考虑安全性和透明性。
在类层次结构的根部定义子节点管理接口的方法具有良好的透明性,但是这一方法是以安全性为代价的,因为客户有可能会做一些无意义的事情,例如在 Leaf 中 Add 对象等。
在 Composite 类中定义管理子部件的方法具有良好的安全性,但是这又损失了透明性,因为 Leaf 和 Composite 具有不同的接口。
1 namespace CompositePattern.Implementation1 2 { 3 public abstract class Component 4 { 5 protected List<Component> _children = new List<Component>(); 6 7 public abstract void Operation(); 8 9 public virtual void Add(Component component) 10 { 11 _children.Add(component); 12 } 13 14 public virtual void Remove(Component component) 15 { 16 _children.Remove(component); 17 } 18 19 public virtual IEnumerable<Component> GetChildren() 20 { 21 return _children; 22 } 23 } 24 25 public class Leaf : Component 26 { 27 public override void Operation() 28 { 29 // do something 30 } 31 32 public override void Add(Component component) 33 { 34 throw new InvalidOperationException(); 35 } 36 37 public override void Remove(Component component) 38 { 39 throw new InvalidOperationException(); 40 } 41 42 public override IEnumerable<Component> GetChildren() 43 { 44 throw new InvalidOperationException(); 45 } 46 } 47 48 public class Composite : Component 49 { 50 public override void Operation() 51 { 52 foreach (var child in _children) 53 { 54 child.Operation(); 55 } 56 // may do something 57 } 58 } 59 60 public class Client 61 { 62 public void TestCase1() 63 { 64 Component component1 = new Leaf(); 65 Component component2 = new Composite(); 66 67 component2.Add(component1); 68 69 component1.Operation(); 70 component2.Operation(); 71 } 72 } 73 }
设计模式之美:Composite(组合),布布扣,bubuko.com
原文:http://www.cnblogs.com/gaochundong/p/design_pattern_composite.html