Understanding design Patterns (combinations)

Source: Internet
Author: User

The combined mode ensures that the combined objects of the same class in the system are consistent with the single object.

Specific environment

Several object Tree schemas with * * whole--Partial ' hierarchy

Where you manipulate a composite structure of any number of objects as if you were manipulating a single object

A class of questions

How to organize and how the parts are treated consistently

Solution Solutions

Combo mode has 3 roles:

  • Component Interface Component:
    Canonical class behavior, generally adding or deleting leaf nodes or combination nodes, implemented by interfaces or abstract classes

  • Leaf node leaves:
    Additions and deletions to the role is not valid , implement Componet, generally have a specific subclass implementation

  • Combined node Composite:
    Package leaf node Reference set, implement adding and removing logic, implement componet, generally implemented by concrete subclasses

Instance Scenario

file system, folder and file additions and deletions operation logic is the same

The company's personnel structure, the distribution of wages and other logical same

Wait a minute

Advantages and Disadvantages

Pros:

  • Treats single and grouped objects consistently, simplifies operations

  • Combine objects with arbitrary additions without modifying source code

Cons:

  • Easily unify business logic specific to single or composite objects
Reference

java combination mode, Author: Fred

Java Design pattern combination mode (Composite), Author: edison_cool911

Understanding design Patterns (combinations)

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.