Index intent structure participant applicability disadvantage effect-related mode implementation method (1): define public interfaces in Component to maintain transparency but compromise security. It is intended to combine objects into a tree structure to represent the "part-whole" hierarchy. Composite makes the use of a single object and a Composite object consistent. Compose objects into tree structures to represent part-whole hierarchies. When appropriate, implement the default behavior of all class common interfaces to declare a sub-Component that is used to access and manage Component. Define an interface in a recursive structure to access a parent component and implement it as appropriate. Leaf indicates the Leaf node object in the combination. The Leaf node does not have any subnodes. Defines the behavior of the element object in the combination. Composite defines the behavior of those parts with child parts. Perform operations related to sub-parts in the Composite interface. The Client uses the Component interface to manipulate the object of the Component. APPLICABILITY The Composite mode can be used in the following scenarios: You want to represent the object's "partial-overall" hierarchy. You want to ignore the differences between a composite object and a single object. You will use all objects in the composite structure in a unified manner. Disadvantages conflict with the class hierarchy design principles one of the purposes of the Composite mode is to make users do not know that they are using specific Leaf and Composite classes. To achieve this goal, Component needs to define some common operations for Leaf and Composite and provide default implementations, and the Leaf and Composite sub-classes can redefine them. However, this goal conflicts with the class hierarchy design principle, which stipulates that a class can only define operations that are meaningful to its subclass. The effect defines the class hierarchy containing basic objects and composite objects. Simplify customer code. This makes it easier to add new types of components. Make your design more general. The related mode Command mode describes how to use a MacroCommand Composite class to form some Command objects and sort them. The "part-parent part" connection is usually used in the Responsibility of Chain mode. The Decorator mode is often used with the Composite mode. They usually have a common parent class. Flyweight allows you to share components, but you can no longer reference their parent parts. Iterator can be used to traverse Composite. Visitor will be originally distributed in the Composite and Leaf classes for operation and behavior localization. Implementation Method (1): define public interfaces in Component to maintain transparency but minimize security. To define Add and Remove operations in Component, you must consider security and transparency. The method for defining the sub-node management interface at the root of the class hierarchy has good transparency, but this method is at the cost of security, because the customer may do some meaningless things, for example, Add an object in Leaf. Defining the management of sub-parts in the Composite class has good security, but this reduces transparency because Leaf and Composite have different interfaces. Copy code 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: component26 {27 public override void Operation () 28 {29 // do something30} 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: Component49 {50 public override void Operation () 51 {52 foreach (var child in _ children) 53 {54 child. operation (); 55} 56 // may do something57} 58} 59 60 public class Client61 {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}