The combination mode of structural design pattern

Source: Internet
Author: User

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

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.