One, C # design pattern: Combination mode (Composite pattern)
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_9. Combination mode {//The combined pattern is primarily used to deal with a class of objects that have "container characteristics"-that is, they act as objects and can contain many other objects as containers. //Combine patterns to combine objects into a tree structure to represent a "partial-whole" hierarchy, combining patterns that allow users to have consistent use of individual objects and composite objects classProgram {Static voidMain (string[] args) {People P=NewPeople ("Start");//define a root containerChinese e =NewChinese ("Chinese"); P.add (e); //Add a node to a containerUsa E1=NewUsa ("American"); P.add (E1); //Add a node to a containerpeople P2=NewPeople ("Earth Man");//defines a container that is understood to be loaded in the root containerP2. ADD (e);//Add a node to a containerP2. ADD (E1);//Add a node to a containerpeople P3=NewPeople ("Earth Man");//define a container that is understood to be contained in a P2 containerP3. ADD (e);//Add a node to a containerP3. ADD (E1);//Add a node to a containerP2. ADD (p3);//Add a node to a containerp.add (p2); //Add a node to a containerP.say (1);//Show All Nodes } } Public Abstract classEvent { Public Abstract voidSay (inta); } Public classchinese:event { Public stringName {Get;Set; } PublicChinese (stringname) {Name=name; } Public Override voidSay (inta) {Console.WriteLine (NewString ('-', a) + Name +": We speak Chinese"); } } Public classusa:event { Public stringName {Get;Set; } PublicUsa (stringname) {Name=name; } Public Override voidSay (inta) {Console.WriteLine (NewString ('-', a) + Name +": We speak English"); } } Public classPeople:event//people is an object is also a container that will assemble the combination we want{List<Event> list =NewList<event> ();//defines a collection of nodes that can be stored Public stringName {Get;Set; } PublicPeople (stringname) {Name=name; } Public Override voidSay (inta) {Console.WriteLine (NewString ('-', a) +Name); foreach(varIteminchlist) {Item. Say (A+2); } } Public voidAdd (Event ev) {list. ADD (EV); } Public voidRemove (Event ev) {list. Remove (EV); } }}
Two, let's look at the output:
1 "In the above code we have added three containers such as the beginning, the Earth people and the Earth people, so we have a four-layer structure of the tree collection, when we want to execute such a complex structure, we only need to add a people container, this container has nodes, and this time the node can be infinitely added, if we need multi-layered structure is We need to add a people container to the node so that we can implement infinite levels of complex structures
2 "Of course, if we only need to use level two, we can only add nodes to the container. For example: Every person has to talk, but we have n individual species, we can not be every kind of object is an instance once, we could use this combination mode, we add each species to the container, then we add n personal species to the container, All we need to do is call the people container's say method to get all the talk events done.
C # design Pattern: Combined mode (Composite pattern)