The Composite pattern
The Composite pattern allows complex tree structures to be built from simple components, often called nodes. A node with children will behave like a container; a node without children will behave like a single object. A composite object is – generally – a container object, where the content may be another composite object.
Traditionally, each node 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 shows this elegant parallelism as a some_action()
method:
Figure 12.9: The Composite pattern
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:
Figure 12.10: A large Composite pattern...