Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects. Composite became a pretty popular solution for the most problems that require building a tree structure.
-
The Component interface describes operations that are common to both simple and complex elements of the tree.
-
The Leaf is a basic element of a tree that doesn’t have sub-elements. Usually, leaf components end up doing most of the real work, since they don’t have anyone to delegate the work to.
-
The Container (aka composite) is an element that has sub-elements: leaves or other containers. A container doesn’t know the concrete classes of its children. It works with all sub-elements only via the component interface. Upon receiving a request, a container delegates the work to its sub-elements, processes intermediate results and then returns the final result to the client.
-
The Client works with all elements through the component interface. As a result, the client can work in the same way with both simple or complex elements of the tree.