Structure |
|
Intention |
Combines objects into a tree structure to represent a "partial-whole" hierarchy. C o m P o s i t e makes the user consistent with the use of a single object and a composite object. |
Applicability |
- You want to represent the part of the object-the overall hierarchy.
- You want users to ignore the difference between a combined object and a single object, and the user will use all the objects in the composite structure uniformly.
|
1 usingSystem;2 usingSystem.Collections;3 4 Abstract classComponent5 {6 protected stringStrName;7 8 PublicComponent (stringname)9 {TenStrName =name; One } A - Abstract Public voidAdd (Component c); - the Public Abstract voiddumpcontents (); - - //Other operations for delete, get, etc. - } + - classcomposite:component + { A PrivateArrayList componentlist =NewArrayList (); at - PublicComposite (strings):Base(s) {} - - Override Public voidAdd (Component c) - { - Componentlist.add (c); in } - to Public Override voiddumpcontents () + { - //First dump the name of this composite node theConsole.WriteLine ("Node: {0}", strName); * $ //Then loops through children, and get then to dump their contentsPanax Notoginseng foreach(Component Cinchcomponentlist) - { the c.dumpcontents (); + } A } the } + - classleaf:component $ { $ PublicLeaf (strings):Base(s) {} - - Override Public voidAdd (Component c) the { -Console.WriteLine ("cannot add to a leaf");Wuyi } the - Public Override voiddumpcontents () Wu { -Console.WriteLine ("Node: {0}", strName); About } $ } - - /// <summary> - ///Summary description for Client. A /// </summary> + Public classClient the { - Component setuptree () $ { the //Here we had to create a tree structure, the //consisting of composites and Leafs. theComposite root =NewComposite ("Root-composite"); the Composite Parentcomposite; - Composite Composite; in leaf leaf; the theParentcomposite =Root; AboutComposite =NewComposite ("First Level-first sibling-composite"); the Parentcomposite. ADD (composite); theleaf =NewLeaf ("First Level-second sibling-leaf"); the Parentcomposite. Add (leaf); +Parentcomposite =Composite; -Composite =NewComposite ("Second Level-first sibling-composite"); the Parentcomposite. ADD (composite);BayiComposite =NewComposite ("Second Level-second sibling-composite"); the Parentcomposite. ADD (composite); the - //we'll leaf the second level-first sibling empty, and start - //populating the second Level-second sibling theParentcomposite =Composite; theleaf =NewLeaf ("Third Level-first sibling-leaf"); the Parentcomposite. Add (leaf); the -leaf =NewLeaf ("Third Level-second sibling-leaf"); the Parentcomposite. Add (leaf); theComposite =NewComposite ("Third Level-third Sibling-composite"); the Parentcomposite. ADD (composite);94 the returnRoot; the } the 98 Public Static intMain (string[] args) About { - Component Component;101Client C =NewClient ();102Component =C.setuptree ();103 104 component. Dumpcontents (); the return 0;106 }107}
Combination Mode
The combination mode of structural design pattern