Composite Pattern
Composite Pattern, also called the Part-Whole Pattern, is used to treat a group of similar objects as a single object. Composite Pattern combines objects according to a tree structure to represent part-whole hierarchies. This type of design pattern is a structural pattern; it creates a tree structure of object groups.
This pattern creates a class that contains a group of its own objects. The class provides a way to modify the same group of objects.
We use the following example to demonstrate the usage of the Composite pattern. The example demonstrates the hierarchical structure of employees in an organization.
Introduction
intent
Combine objects into tree structures to represent "part-whole" hierarchies. Composite Pattern makes the use of individual objects and composite objects consistent for users.
Main problems solved
- Simplify the handling of objects in a tree structure, whether they are individual objects or composite objects.
- Decouple client code from the internal structure of complex elements, so that clients can handle all types of nodes uniformly.
Use Case
- When there is a need to represent hierarchical structures of objects, such as file systems or organizational structures.
- When you want client code to handle all objects in a tree structure in a consistent way.
Implementation Approach
- Uniform interfaceDefine an interface that all objects (branches and leaves) implement.
- Composite StructureBranch objects contain a list of references to the interface; these references can be leaves or branches.
Key code
- Component InterfaceDefine the operations that all objects must implement.
- Leaf classImplement the Component interface and represent leaf nodes in the tree.
- Composite classAlso implement the Component interface and contain a collection of other Component objects.
Application example
- Arithmetic expressionBuild a tree structure composed of operands, operators, and subexpressions.
- GUI componentsIn Java's AWT and Swing libraries, containers (such as Panel) can contain other components (such as buttons and checkboxes).
Advantages
- Simplify client codeClients can handle all types of nodes uniformly.
- Easy to ExtendNew leaf types or branch types can be easily added.
Disadvantages
- Violating the Dependency Inversion PrincipleComponent declarations are based on concrete classes rather than interfaces, which may reduce code flexibility.
Usage suggestions
- When designing, prefer interfaces over concrete classes to improve system flexibility and maintainability.
- Suitable for scenarios that need to handle complex tree structures, such as file systems, organizational structures, etc.
Notes
- When implementing, ensure all components follow a unified interface to maintain consistency.
- Consider using the Factory Pattern to create different types of components, to further decouple component creation logic.
Structure
The core roles of the Composite pattern include:
Component:
- Defines a common interface for all objects in the composition, which can be an abstract class or an interface. It declares methods for accessing and managing child components, including adding, deleting, and getting child components, etc.
Leaf node (Leaf):
- Represents leaf node objects in the composition; leaf nodes have no children. It implements the Component interface methods but generally does not contain child components.
Composite node (Composite):
- Represents composite objects in the composition. Composite nodes can contain child nodes, which may be leaf nodes or other composite nodes. It implements the Component interface methods, including methods for managing child components.
Client:
- Interacts with the composite structure through the Component interface. Clients do not need to distinguish between leaf nodes and composite nodes and can treat wholes and parts consistently.
Implementation
We have a classEmployee, this class is treated as the composite model class.CompositePatternDemoClass usageEmployeeClass to add the department hierarchy and print all employees.
Step 1
CreateEmployeeclass, which hasEmployeea list of objects.
Employee.java
Step 2
UsageEmployeeClass to create and print the employee hierarchy.
CompositePatternDemo.java
Step 3
Run the program, the output is:
Employee :[ Name : John, dept : CEO, salary :30000 ] Employee :[ Name : Robert, dept : Head Sales, salary :20000 ] Employee :[ Name : Richard, dept : Sales, salary :10000 ] Employee :[ Name : Rob, dept : Sales, salary :10000 ] Employee :[ Name : Michel, dept : Head Marketing, salary :20000 ] Employee :[ Name : Laura, dept : Marketing, salary :10000 ] Employee :[ Name : Bob, dept : Marketing, salary :10000 ]other extensions