The composite pattern allows complex tree-like structures to be built from simple components. These components, called composite objects, are able to behave sort of like a container and sort of like a variable, depending on whether they have child components. Composite objects are container objects, where the content may actually be another composite object.
Traditionally, each component in a composite object must be either a leaf node (that cannot contain other objects) or a composite node. The key is that both composite and leaf nodes can have the same interface. The following UML diagram is very simple:
![](https://static.packt-cdn.com/products/9781789615852/graphics/assets/9fa1eb55-b038-4c5b-b10c-c5f34bf86e4d.png)
This simple pattern, however, allows us to create complex arrangements of elements, all of which satisfy the interface of the component object. The following diagram depicts a concrete instance of such a complicated arrangement:
![](https://static.packt-cdn.com/products/9781789615852/graphics/assets/99c562f9-c96b-4219-874e-c832fc7aa6ea.png)
The composite pattern...