Problem: there are hierarchies with arbitrary depth and width (e.g. folders and files)
Solution: the composite pattern lets a Client treat an individual class called Leaf and Compositions of Leaf classes uniformly
Composite - structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.
<aside>
Applicability
- Using the Composite pattern makes sense only when the core model of you app can be represented as a tree
- Use the pattern when you want the client code to treat both simple and complex elements uniformly.
</aside>


The greatest benefit of this approach is that you dont need to care about the concrete classes ob objects that compose the tree. We can treat them all the same via the common interface.
Structure

- describes operations that are common to both simple and complex elements of the tree
- basic element, doesnt have sub-elements
- Composite (aka Container), element that has sub-elements: leafs or other containers. Container doesnt know the concrete classes of its children. Works with sub-elements only via the component interface
- works with all elements through the component interface: can work that way with both simple or complex elements of the tree.

<aside>
How to implement:
- Make sure that the core model of your app can be represented as a tree structure. Try to break it down into simple elements and containers. Remember that containers must be able to contain both simple elements and other containers.
- Declare the component interface with a list of methods that make sense for both simple and complex components.
- Create a leaf class to represent simple elements. A program may have multiple different leaf classes.
- Create a container class to represent complex elements. In this class, provide an array field for storing references to sub-elements. The array must be able to store both leaves and containers, so make sure it’s declared with the component interface type.
While implementing the methods of the component interface, remember that a container is supposed to be delegating most of the work to sub-elements.
- Finally, define the methods for adding and removal of child elements in the container.
Keep in mind that these operations can be declared in the component interface. This would violate the Interface Segregation Principle because the methods will be empty in the leaf class. However, the client will be able to treat all the elements equally, even when composing the tree.
</aside>