Definition: composite design pattern follows a single way of organizing the hierarchy of objects, and here a group of objects is treated as single objects. The hierarchy is represented as a tree structure to reduce the complexity.
Client: It is an entity that wants to communicate with various existing objects or a single object.
Composite element: The element placed inside the hierarchy but not the leaf element is called a composite element. It cannot reside on the bottom level.
Leaf element: Elements placed at the bottom of the hierarchy and have no further extension of objects inside are all leaf elements.
Note- Here C represents composite elements and L represents leaf elements
When to use composite design pattern :
- When representation of the whole hierarchy is required.
- Presence of Several objects which are connected to the root folder.
- When the difference between the composition of objects and individual objects is to be ignored, all structures should follow uniformity.
This is one of the variations of the structural design patterns. Here objects are operated in a generic manner that may or may not represent a hierarchy of classes mainly, objects are described in the form of trees, and the tree nodes represent the whole hierarchy. It is used to solve many significant and cunning problems. This is used when the user wants a single object to follow a similar pattern, and all the objects will follow a similar pattern. Tree structure helps to maintain the uniformity of the objects .it describes a single way of construction of classes of two different hierarchies. It reduces coupling.
If you have a large hierarchy of classes, you have to reduce dependencies among the classes; otherwise, you will have to update values again and again.
Let's talk about one real-life and mostly used example of a composite design pattern: A File system. A file system contains a large hierarchy of objects. It contains a folder within the folder and also files within the folder. Files can be documents, excel copies, movies, audios, etc., and they all might reside within a media folder. This media folder might reside within data, and this data folder might also be within any related folder, and this hierarchy continues. If you are dealing with multiple components, you will create structural classes that follow the same pattern to add more components to the class. Adding components is very simple because of its structural hierarchy.
Problem statement :
Suppose you have to go to a birthday party, you went to a shop and bought a few gifts. The shopkeeper wrapped all the gifts and put all the boxes into one big box. But a problem arose the shopkeeper forgot to check the price of the items! Now, what will he do? He has no option but to unwrap all the boxes and check the price, and after that, he again wraps all the objects and puts them back in the big box. But this solution is only invalid in real life in programming. You cannot do this by running a single loop; you should know all the products and their classes before accessing them, you have to go through all the nesting levels and all the unnecessary details, so this method will not work out for knowing the prices.
A composite design pattern helps work with a single interface that will deal with both box and product for a desirable result. For this problem, it will access all the available boxes, then check all product prices, and then it can help to calculate the overall prices. This solution is only for programming, not for real-world practice.
A composite design pattern makes the structure flexible since we can easily add or remove any node. We can add new types of elements, but they must follow the same pattern. We can use advanced algorithms such as iteration, recursion, and polymorphism to obtain the tree structure, and it does not violate the open-closed principle. It can group or ungroup even manipulations in multiple objects in run time which is a huge benefit of using a composite design pattern.
Note - Composite design pattern can only be used in the hierarchy of classes which can be represented in tree format.