Composite Pattern
The Composite Pattern, also called the Part-Whole pattern, is used to treat a group of similar objects as a single object. The Composite Pattern composes objects based on a tree structure to represent part as well as whole hierarchies. This type of design pattern belongs to the structural patterns, and it creates a tree structure of groups of objects.
This pattern creates a class that contains its own group of objects. This class provides ways to modify the same group of objects.
We demonstrate the use of the Composite Pattern with the example below. The example demonstrates the hierarchy of employees in an organization.
Introduction
Intent
Compose objects into tree structures to represent "part-whole" hierarchies. The Composite Pattern makes the use of individual objects and compositions of objects consistent for the client.
Main problem solved
- Simplify the handling of objects in a tree structure, whether they are individual objects or composite objects.
- Decouple the client code from the internal structure of complex elements, so that the client can handle all types of nodes uniformly.
Use Cases
- When you need to represent a hierarchy of objects, such as a file system or an organizational structure.
- When you want the client code to be able to handle all objects in a tree structure in a consistent way.
Implementation
- Uniform Interface: define an interface that all objects (branches and leaves) implement.
- Composite structure: a branch object contains a list of references to an interface, and these references can be leaves or branches.
Key code
- Component interface: defines the operations that all objects must implement.
- Leaf class: implements the Component interface and represents the leaf nodes in the tree.
- Composite class: also implements the Component interface and contains a collection of other Component objects.
Application Example
- Arithmetic expressions: build a tree structure consisting of operands, operators, and sub-expressions.
- GUI components: in Java's AWT and Swing libraries, a container (such as Panel) can contain other components (such as buttons and checkboxes).
Advantages
- Simplifies client code: the client can handle all types of nodes uniformly.
- Easy to extend: new leaf types or branch types can be added easily.
Disadvantages
- Violates the Dependency Inversion Principle: the declaration of components is based on concrete classes rather than interfaces, which may reduce the flexibility of the code.
Usage recommendation
- When designing, prefer interfaces over concrete classes to improve the flexibility and maintainability of the system.
- Suitable for scenarios that need to handle complex tree structures, such as file systems and organizational structures.
Notes
- When implementing, ensure that all components follow a unified interface to maintain consistency.
- Consider using the Factory pattern to create different types of components to further decouple the creation logic of components.
structures
The core roles of the Composite Pattern include:
-
Component:
- Defines the common interface for all objects in the composition; it can be an abstract class or an interface. It declares methods for accessing and managing child components, including add, remove, get child, and so on.
-
Leaf:
- Represents the leaf node objects in the composition; a leaf node has no child nodes. It implements the methods of the Component interface but usually does not contain child components.
-
Composite:
- Represents the composite objects in the composition; a composite node can contain child nodes, which can be leaf nodes or other composite nodes. It implements the methods of the Component interface, including methods for managing child components.
-
Client (Client):
- By interacting with the composite structure through the Component interface, the client does not need to distinguish between leaf nodes and composite nodes, and can treat the whole and the part consistently.
Create a class that implements
We have a class Employeewhich is treated as the composite model class.CompositePatternDemo Class uses Employee class to add the department hierarchy and print all employees.
Step 1
creation Employee class, which has a Employee list of objects.
Employee.java
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
Employee
{
private
String
name
;
private
String
dept
;
private
int
salary
;
private
List
<
Employee
subordinates;
//
constructor
public
Employee
(
String
name
,
String
dept
,
int
sal
)
{
this
.
name
=
name
;
this
.
dept
=
dept
;
this
.
salary
=
sal
;
subordinates
=
new
ArrayList
<
Employee
(
);
}
public
void
add
(
Employee
e
)
{
subordinates
.
add
(
e
)
;
}
public
void
remove
(
Employee
e
)
{
subordinates
.
remove
(
e
)
;
}
public
List
<
Employee
getSubordinates
(
)
{
return
subordinates;
}
public
String
toString
(
)
{
return
(
"
Employee :[ Name :
"
name
"
, dept :
"
dept
"
, salary :
"
salary
"
]
"
)
;
}
}
Step 2
Use Employee class to create and print the employee hierarchy.
CompositePatternDemo.java
public
class
CompositePatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
Employee
CEO
=
new
Employee
(
"
John
"
,
"
CEO
"
,
30000
)
;
Employee
headSales
=
new
Employee
(
"
Robert
"
,
"
Head Sales
"
,
20000
)
;
Employee
headMarketing
=
new
Employee
(
"
Michel
"
,
"
Head Marketing
"
,
20000
)
;
Employee
clerk1
=
new
Employee
(
"
Laura
"
,
"
Marketing
"
,
10000
)
;
Employee
clerk2
=
new
Employee
(
"
Bob
"
,
"
Marketing
"
,
10000
)
;
Employee
salesExecutive1
=
new
Employee
(
"
Richard
"
,
"
Sales
"
,
10000
)
;
Employee
salesExecutive2
=
new
Employee
(
"
Rob
"
,
"
Sales
"
,
10000
)
;
CEO
.
add
(
headSales
)
;
CEO
.
add
(
headMarketing
)
;
headSales
.
add
(
salesExecutive1
)
;
headSales
.
add
(
salesExecutive2
)
;
headMarketing
.
add
(
clerk1
)
;
headMarketing
.
add
(
clerk2
)
;
//
Print all employees of the organization
System
.
out
.
println
(
CEO
)
;
for
(
Employee
headEmployee
:
CEO
.
getSubordinates
(
)
)
{
System
.
out
.
println
(
headEmployee
)
;
for
(
Employee
employee
:
headEmployee
.
getSubordinates
(
)
)
{
System
.
out
.
println
(
employee
)
;
}
}
}
}
Step 3
Execute the program; the output result 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 ]