A Composite Pattern says that just "allow clients to operate in generic manner on objects that may or may not represent a hierarchy of objects".
Advantage of Composite Design Pattern
Usage of Composite Pattern
It is used:
UML for Composite Pattern
Elements used in Composite Pattern:
Let's see the 4 elements of composte pattern.
Note:The work flow of above general UML is as follows.
Client uses the component class interface to interact with objects in the composition structure. If recipient is the leaf then request will be handled directly. If recipient is a composite, then it usually forwards the request to its child for performing the additional operations.
Example of Composite Pattern
We can easily understand the example of composite design pattern by the UML diagram given below:
Implementation of above UML:
Create an Employee interface that will be treated as a component.
Create a BankManager class that will be treated as a Composite and implements Employee interface.
Create a Cashier class that will be treated as a leaf and it will implement to the Employee interface.
Create a Accountant class that will also be treated as a leaf and it will implement to the Employee interface.
Create a CompositePatternDemo class that will also be treated as a Client and ii will use the Employee interface.