Implementing the Composite design pattern
The Composite design pattern is another structural GoF pattern that helps us manage complex object structures.
Goal
The goal behind the Composite pattern is to create a hierarchical data structure where you don’t need to differentiate groups of elements from a single element, making the hierarchy easy to use for its consumers.
You could think of the Composite pattern as a way of building a graph or a tree with self-managing nodes.
Design
The design is straightforward; we have components and composites. Both implement a common interface that defines the shared operations. The components are the single nodes, while the composites are collections of components. Let’s take a look at a diagram:
Figure 9.7: Composite class diagram
In the preceding diagram, Client
depends on an IComponent
interface. By doing so, it is unaware of the implementation it is using; it could be a Component
or a Composite
....