Card 08/ 08
Recap: Composite Pattern
One interface for parts and wholes, and the whole forwards to its parts.
Composite gives single items and groups one interface, so a tree of any depth answers a call the way a single item does.
- Intent. Compose objects into trees to represent part-whole hierarchies, and treat single objects and compositions uniformly.
- Participants. The component interface, the leaf, the composite that holds components, and the client.
- The recursion. A composite calls the operation on each child and combines the results, so depth never appears in the code.
- In the wild. Swing's
Containerholds components, and the browser's DOM is the same tree. - The trade-off. Child methods on the component give transparency and run-time refusals; on the composite alone they give safety and a client that must know the node's kind.
- Against Decorator. Both compose recursively; a decorator wraps one object, a composite holds many.
← → move through sections and cards