What is the intent of the composite pattern ?
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
What is the motivation of the composite pattern ?
Avoid code complexity because of distinguishing classes of similar and composed objects on which the same operations are applied (on individual objects and on their composition).
The key to the Composite pattern is an abstract class that represents both primitives and their containers.
What is the applicability of the composite pattern ?
You want to represent part-whole hierarchies of objects.
You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.
What are the participants of the composite pattern ?
What is the role of the ‘component’ participant in the composite model ?
– declares the interface for objects in the composition.
– implements default behavior for the interface common to all classes, as appropriate.
– declares an interface for accessing and managing its child components.
– (optional) defines an interface for accessing a component’s parent in the recursive structure, and implements it if that’s appropriate.
What is the role of the ‘leaf’ participant in the composite model ?
– represents leaf objects in the composition. A leaf has no children.
– defines behavior for primitive objects in the composition.
What is the role of the ‘composite’ participant in the composite model ?
– defines behavior for components having children.
– stores child components.
– implements child-related operations in the Component interface.
What is the role of the ‘client’ participant in the composite model ?
– manipulates objects in the composition through the Component interface.
What are the consequences of using the “composite” pattern ?
Describe the collaborations between the composite pattern participants ?
Clients use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.
What are the questions/tradeoff to answer during implementation of the composite pattern ?
What are the composite related patterns ?
Often the component-parent link is used for a Chain of Responsibility (223).
Decorator (175) is often used with Composite.
When decorators and composites are used together, they will usually have a common parent class.
So decorators will have to support the Component interface with operations like Add, Remove, and GetChild.
Flyweight (195) lets you share components, but they can no longer refer to their parents. Iterator (257) can be used to traverse composites.
Visitor (331) localizes operations and behavior that would otherwise be distributed across Composite and Leaf classes.