C # design Pattern: Combined mode (Composite pattern)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.