Composite Design Pattern suitable for cases like hierarchical structure which is like a tree structure. At least single Common behavior with all objects of the hierarchy is essential. This pattern comes under a structural pattern and to implement Composite Design Pattern there should be below basic requirements.
- Application with hierarchical structure
- There should be common functionality throughout the structure.
- Objects are treated uniformly as individuals or in a composite way.
There are two types of nodes in a tree structure
- Composite: There are objects/objects under this object.
- Leaf: There is no child object under this object.
Let’s take an example to clear this easily.
Composite Design Pattern Real World Example
Composite Pattern examples are not difficult to find in the real world. Consider the software development company and there are employees working for the company. But employees held their own position and each position has a level of post hierarchy.
For easiness let’s take 3 positions as CEO, Architects under CEO, and Developers under Architects. As a common behavior, all employees need to report for their own daily work. Report to work is common behavior of all employees and its not depend on the position.
Let’s see the class diagram for the above example.
According to the diagram, There are four main components in Composite Design Pattern. Those are
- Component: Include default behavior. Declare as an interface to access(Inherited) by all the Classes throughout the tree structure. (Employee)
- Leaf: Tail objects in the hierarchy and there are no children. Requests are handled immediately because of its at end of the Composite. (Software Developer)
- Composite: Inherited default behavior Implemented and held children by Composite objects. (Ceo. Software Architect)
- Client: Composite structure objects manipulated by Client. The client executes the Composite default behavior.
Let’s move into the java code for the example.
Composite Design Pattern in Java
import java.util.ArrayList; import java.util.List; // Component with common behaviour interface Employee{ void reportToDailyWork(); } // Composite object(Ceo) held SoftwareArchitect as child nodes class Ceo implements Employee{ private String employeeId; private String position; private List<Employee> employee=new ArrayList<Employee>(); public Ceo(String employeeId, String position) { this.employeeId = employeeId; this.position = position; } public void addEmployee(Employee e){ employee.add(e); } public void removeEmployee(Employee e){ employee.remove(e); } @Override public void reportToDailyWork() { System.out.println("Report to daily work by Employee Id" +employeeId+" position "+ position); for (Employee e : employee) { e.reportToDailyWork(); } } } // Composite object(SoftwareArchitect) held SoftwareDeveloper as child nodes class SoftwareArchitect implements Employee{ private String employeeId; private String position; private List<Employee> employee=new ArrayList<Employee>(); public SoftwareArchitect(String employeeId, String position) { this.employeeId = employeeId; this.position = position; } public void addEmployee(Employee e){ employee.add(e); } public void removeEmployee(Employee e){ employee.remove(e); } @Override public void reportToDailyWork() { System.out.println("Report to daily work by Employee Id" +employeeId+" position "+ position); for (Employee e : employee) { e.reportToDailyWork(); } } } // Leaf node only execute default class SoftwareDeveloper implements Employee{ private String employeeId; private String position; public SoftwareDeveloper(String employeeId, String position) { this.employeeId = employeeId; this.position = position; } @Override public void reportToDailyWork() { System.out.println("Report to daily work by Employee Id" +employeeId+" position "+ position); } } public class CompositeClient { public static void main(String[] args) { Ceo ceo= new Ceo("1001", "CEO"); SoftwareArchitect architect1= new SoftwareArchitect("2001", "Software Architect"); SoftwareArchitect architect2= new SoftwareArchitect("2002", "Software Architect"); SoftwareDeveloper developer1= new SoftwareDeveloper("3001", "Software Developer"); SoftwareDeveloper developer2= new SoftwareDeveloper("3002", "Software Developer"); SoftwareDeveloper developer3= new SoftwareDeveloper("3003", "Software Developer"); SoftwareDeveloper developer4= new SoftwareDeveloper("3004", "Software Developer"); ceo.addEmployee(architect1); ceo.addEmployee(architect2); architect1.addEmployee(developer1); architect1.addEmployee(developer2); architect1.addEmployee(developer3); architect2.addEmployee(developer4); ceo.reportToDailyWork(); } }
Output
Report to daily work by Employee Id1001 position CEO Report to daily work by Employee Id2001 position Software Architect Report to daily work by Employee Id3001 position Software Developer Report to daily work by Employee Id3002 position Software Developer Report to daily work by Employee Id3003 position Software Developer Report to daily work by Employee Id2002 position Software Architect Report to daily work by Employee Id3004 position Software Developer
Advantages of Composite Design Pattern
- Easy to add a new node to the structure
- Easy to run common functionality as a whole.
Design Patterns Book
Design Patterns are one of the most famous solutions in programming and millions of people follow them to fix their tasks, design projects, and so on. Most of the patterns are created based on basic OOP concepts (If you beginner then read our basic OOP, Encapsulation, Inheritance, Polymorphism, and Abstraction articles with great examples )
There are a lot of articles and design pattern books available with different examples to read. Other than our article if you are interested in this topic you could go through the below books as well.
Books | Description |
---|---|
Head First Design Patterns: A Brain-Friendly Guide | My favorite and it’s more readable. There are good examples and they look good to beginners as well. |
Design Patterns: Elements of Reusable Object-Oriented Software | There are 23 design patterns. showing how to select an appropriate pattern for your case. |
Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software | There are a lot of simple examples showing how to use design patterns in the correct way. Those are based on SOLIC principles. |
There are a lot of design pattern ebooks on Amazon. Those design patterns are written with great examples in a different programming language. I included a few of them according to their best ranking.