Essentials and models of software design (6)-Combination Mode

Source: Internet
Author: User

The merging mode is sometimes called part-whole ). The merging mode organizes objects into the tree structure and can be used to describe the relationship between the whole and the part. The merging mode allows the client to view elements as compound elements. The implementation of the merging mode is divided into two forms based on the differences between the Implemented interfaces, namely the security mode and the transparent mode. The merging mode does not provide a parent object management method, but the merging mode must provide sub-object management methods (such as add, remove, and getchild) where appropriate ).

Transparent Mode

As the first option, declare all methods in component to manage subclass objects, including add (), remove (), and getchild () methods. The advantage of this is that all component classes have the same interfaces. In the client's view, the difference between a leaf object and a synthetic object disappears at the interface level, and the client can treat all objects at the same level. This is the synthesis mode in transparent form. The disadvantage of this choice is that it is not safe, because leaf objects and synthetic objects are essentially different. Leaf objects cannot have objects of the next level. Therefore, the add (), remove (), and getchild () methods have no meaning. They do not make errors during compilation, errors only occur during running.

So what is a transparent synthesis mode? See composite pattern (combination mode)

 

Security Mode

The second option is to declare all methods in the composite class to manage subclass objects. This approach is safe because leaf objects do not have any method to manage subclass objects. Therefore, if the client uses these methods for leaf objects,ProgramErrors occur during compilation.

The disadvantage of this choice is that it is not transparent, because the leaf class and the synthesis class will have different interfaces. These two forms have their own advantages and disadvantages and need to be decided based on the specific circumstances of the software.

So what is a safe mode of synthesis? See composite pattern (combination mode)

    1. When using the merging mode, several issues are taken into account to clearly give reference to the parent object.
    2. The parent object reference is provided in the sub-object, which can easily traverse all parent objects. With this reference, you can easily apply the responsibility chain mode.
    3. In a general system, you can share components using the metadata mode. However, because the objects in the merging mode often have references to the parent object, sharing is not easy to implement.
    4. Sometimes the system needs to traverse the child component of a tree branch structure for many times. At this time, you can consider storing the result of traversing the child component in the parent component as a cache.
    5. What data types are used to store sub-objects?CodeArraylist is used. Other Clustering or arrays can be used in the actual system. the client tries its best not to directly call methods in the leaf class, but to use the polymorphism of its parent class (Component) to complete the call, which can increase code reusability.

When to adopt the Combination Mode

1. Important requirements and overall hierarchy

2. you want the user to ignore the differences between the composite object and a single object. The user will use all objects in the composite structure in a unified manner: 1. the composite mode uses a tree structure to implement a common object container and convert the "one-to-many" relationship into a "one-to-one" relationship, this allows the customer code to process objects and object containers in a consistent manner without having to worry about processing a single object or a combined object container. decoupling customer code from complex object container structures is the core idea of the composite mode, the Customer Code will be dependent on the pure abstract interface, rather than the complex internal implementation structure of the object container, so as to better "respond to changes ".

3. in composite mode, "add, remove, and other methods related to object containers" are defined in "component class indicating abstract objects, defining it in the "composite class that represents the object container" is a dilemma related to "Transparency" and "security" and requires careful consideration. This may violate the "single Responsibility Principle" of object-oriented systems, but this is a price that must be paid for this special structure. The implementation of ASP. NET controls provides a good demonstration in this respect.

4. In the composite mode, the sub-objects in the parent object can be traced back. If the parent object requires frequent traversal, the cache technique can be used to improve the efficiency.

Key points of the composite mode

    • The composite mode uses a tree structure to implement a common object container, which converts the "one-to-many" relationship into a "one-to-one" relationship, this allows the customer code to process objects and object containers in a consistent manner, without having to worry about processing a single object or a combined object container. Add the "Customer Code and complex object container structure"
    • Decoupling is the core idea of the composite model. After decoupling, the Customer Code will have a dependency relationship with the pure abstract interface, instead of the internal implementation structure of the object container, so as to better cope with changes"
    • In composite mode, "add, remove, and other methods related to object containers" are defined in "component class indicating abstract objects, defining it in the "composite class that represents the object container" is a dilemma related to "Transparency" and "security" and requires careful consideration. This may violate the "single Responsibility Principle" of object-oriented systems, but this is a price that must be paid for this special structure. The implementation of ASP. NET controls provides a good demonstration in this respect.
    • In the specific implementation of the composite mode, sub-objects in the parent object can be traced back. If the parent object requires frequent traversal, you can use the cache technique to improve efficiency. Files of interfaces and implementation classes must be separated. We recommend that you store a class in a file!

For other information, see:

Net Design Pattern (11): composite pattern)

Design Pattern in. Net -- composite pattern

Core design patterns (4) composite Combination Mode

Reference

9. Composite combination (Structural Mode)

It is recommended to read this article.

Composite pattern (Combination Mode)

 

Design Mode Study Notes 17: composite pattern

Related Article

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.