Design Mode-Combination Mode

Source: Internet
Author: User
Composite combines objects into a tree structure to represent a "partial-whole" hierarchy. The combination mode ensures consistency between the use of a single object and a composite object. Class chart code implementation
Abstract class component // abstract Part class description the behavior of all parts in the future {protected string name; // name public component (string name) {This. name = Name;} public abstract void add (component C); // Add the sub-part public abstract void remove (component C ); // Delete the child part public abstract void display (INT depth); // display part structure} // composite part class leaf: component {public leaf (string name ): base (name) {}// the leaf node does not have the function of adding parts, so public override void add (component C) {console. writeline ("cannot add to a leaf");} // same as public override void remove (component C) {console. writeline ("cannot remove from a leaf");} // display its own execution result public override void display (INT depth) {console. writeline (new string ('-', depth) + name) ;}// composite class: component {// Private list of components used to save the combination <component> Children = new list <component> (); Public composite (string name): Base (name) {}// add public override void add (component C) {children. add (c);} // delete part public override void remove (component C) {children. remove (c);} // traverse the subnode and display public override void display (INT depth) {console. writeline (new string ('-', depth) + name); foreach (Component component in children) {component. display (depth + 2) ;}} client code static void main (string [] ARGs) {// construct the root node composite root = new composite ("root "); // Add the leaf node root. add (new leaf ("leaf A"); root. add (new leaf ("leaf B"); // construct a composite comp = new composite ("Composite X"); // Add a leaf node COMP for the combination. add (new leaf ("leaf xa"); comp. add (new leaf ("leaf XB"); root. add (COMP); // same as comp composite comp2 = new composite ("Composite XY"); comp2.add (new leaf ("leaf xya ")); comp2.add (new leaf ("leaf xyb"); root. add (comp2); // Add the leaf node root. add (new leaf ("leaf C"); leaf = new leaf ("leaf D"); root. add (leaf); root. remove (leaf); // display the result root. display (1); console. read ();}
Applicability 1. When requirements reflect the structure of the part and the overall level. 2. If you want users to ignore the differences between the image of a single composite object fish and use all objects in the composite structure in a unified manner. Features exchange a single responsibility design principle for transparency, that is, by allowing the component interface to simultaneously include operations for managing subnodes and leaf nodes, the customer can group and leaf nodes equally. That is to say, whether an element is a combination or a leaf node is transparent to the customer. Sometimes the system needs to traverse the sub-component of a tree branch multiple times. At this time, the traversal results can be cached.

Design Mode-Combination Mode

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.