Applied Design Patterns with Java
Structural :: Composite (163) {C ch 11}
Applicability
Use the Composite pattern when:
- there is a need to represent part-whole hierarchies
of objects;
- clients need to be able to ignore the difference between
compositions of objects and individual objects. Clients will treat all objects in the Composite
structure uniformly.
Structure
The Composite pattern has this general organization:
A typical Composite object structure might look like this:
Participants
- Component (Graphic) declares the interface for objects in the composition, implements
default behavior for the interface common to all classes, declares an interface for accessing and managing its
child components, and it may define an interface for accessing a component's parent in the recursive structure,
and implements it if needed.
- Leaf (Rectangle,
Line, Text, etc.) represents leaf objects
in the composition, has no children, and defines behavior for primitive objects in the composition.
- Composite (Picture) defines behavior for components having children, stores child components,
and implements child-related operations in the Component interface.
- Client
manipulates objects in the composition through the Component interface.
Catalog Structural Prev Next