Composite allows clients to access individual object and composition objects in uniform way.
Sometimes we have to deal with Hierarchical or Tree type of data. The data which are related with each other and form a type of hierarchy. We can write the recursive logic to process related entries. But, the problem with the approach is : client has to distinguish between the type of objects, weather it is a end node or collection of nodes. It would become more complex as, we need to do this distinction all the time when we work with those objects. So, composite design pattern shows how to use recursive composition so that client doesn’t have to distinguish between the objects.
Design Time Consideration:
We should follow these steps to achieve Composite Design Pattern.
- Create an Abstract Class
- Inherit an individual class with certain behavior
- Inherit a composite class which would store the Kids objects. Operation of Composite objects * should actually perform the operations of each child.
- Client should use Component Abstract class to interact with composite objects.