Composite Design Pattern
What is the Composite Design Pattern?
The Composite Design Pattern is a structural design pattern that allows you to treat individual objects and groups of objects in the same way.
In simple words, this pattern helps you build a tree-like structure where both single items and collections of items follow the same rules.
This pattern is very useful when you want to represent a hierarchy such as:
- Folders and files in a file system
- Menus and sub-menus in an application
- Organization structure (employees and departments)
Real-Life Example
Think about a company structure:
- An Employee can be a single person
- A Department can contain multiple employees or even other departments
Whether it is a single employee or a department, we want to calculate the total salary in the same way. This is where the Composite Design Pattern fits perfectly.
Key Components of Composite Pattern
- Component – Common interface for both individual and composite objects
- Leaf – Represents a single object
- Composite – Represents a group of objects
Java Example of Composite Design Pattern
Step 1: Create a Common Interface (Component)
public interface Employee {
int getSalary();
void showDetails();
}
Step 2: Create a Leaf Class (Individual Object)
public class Developer implements Employee {
private String name;
private int salary;
public Developer(String name, int salary) {
this.name = name;
this.salary = salary;
}
@Override
public int getSalary() {
return salary;
}
@Override
public void showDetails() {
System.out.println("Developer: " + name + ", Salary: " + salary);
}
}
Step 3: Create a Composite Class (Group of Objects)
import java.util.ArrayList;
import java.util.List;
public class Department implements Employee {
private String departmentName;
private List<Employee> employees = new ArrayList<>();
public Department(String departmentName) {
this.departmentName = departmentName;
}
public void addEmployee(Employee employee) {
employees.add(employee);
}
@Override
public int getSalary() {
int totalSalary = 0;
for (Employee employee : employees) {
totalSalary += employee.getSalary();
}
return totalSalary;
}
@Override
public void showDetails() {
System.out.println("Department: " + departmentName);
for (Employee employee : employees) {
employee.showDetails();
}
}
}
Step 4: Test the Composite Pattern
public class CompositePatternDemo {
public static void main(String[] args) {
Employee dev1 = new Developer("Rahul", 50000);
Employee dev2 = new Developer("Anita", 60000);
Department itDepartment = new Department("IT Department");
itDepartment.addEmployee(dev1);
itDepartment.addEmployee(dev2);
itDepartment.showDetails();
System.out.println("Total Salary: " + itDepartment.getSalary());
}
}
Output Explanation
Here, both Developer and Department are treated as Employee. This allows the client code to work with a single interface without worrying about whether it is handling a single object or a group of objects.
Advantages of Composite Design Pattern
- Makes client code simple and clean
- Supports recursive structures easily
- Adds flexibility when working with complex hierarchies
When to Use Composite Design Pattern?
- When you need to represent part-whole hierarchies
- When clients should treat individual and grouped objects the same way
- When your structure looks like a tree
Conclusion
The Composite Design Pattern is a powerful and easy-to-understand pattern that helps manage hierarchical data efficiently. By using a common interface, it ensures consistency and simplifies code, making it ideal for real-world applications like file systems, UI components, and organizational structures.