Structural Design Pattern
![](https://codelido.com/assets/files/2022-12-18/1671387958-7695-single-ton-design-pattern-6.jpg)
Definition
The composite pattern is used when we have to represent a part-whole hierarchy. When we need to create a structure in a way that the objects in the structure have to be treated the same way, we can apply the composite design pattern.
Characterisitics
- It simplifies complex object structures by combining simple objects or data into larger structures.
- It promotes code reusability by allowing objects to be composed from existing objects.
- It allows for an easy way to create objects with complex internal structures.
- It allows for easy extension of existing composite structures.
- It allows for easy customization of existing composite structures.
- It is a great way to represent part-whole hierarchies.
- It is a great way to represent recursive tree structures.
UML Diagram
![](https://codelido.com/assets/files/2022-12-18/1671388068-28145-image.png)
Code
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+" ]");
}
}
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);
}
}
}
}
Output
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 ]
Advantages
- Clients use the Component class interface to interact with objects in the composite structure.
- If call is made to a Leaf, the request is handled directly.
- If call is to a Composite, it forwards the request to its child components.
Disadvantages
- Once tree structure is defined, the composite design makes the tree overly general.
- In specific cases, it is difficult to restrict the components of the tree to only particular types.
- Therefore, to enforce such constraint, the program must rely on run-time checks, since it cannot use the type system the of programming language.