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)