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. You could think of it 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:
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
. Then, we have two implementations:
Component
represents...