Composite Pattern

Summary

  • The Composite pattern arranges structured hierarchies so that single components and groups of components can be treated in the same way.
  • The Composite pattern treats single objects and composite objects in the same way.
  • The Composite pattern has to deal with two types: Components and Composites of those components. Both types agree to conform to an interface of common operations.
  • The Composite pattern has wide applicability and is often used in conjunction with the Decorator, Iterator, and Visitor patterns.

UML diagram: (see Composite Pattern code review)

Composite Pattern class diagram of sample code download

Usage

Use the Composite pattern when:

  • You have an irregular structure of objects and composites of the objects.
  • You want clients to ignore all but the essential differences between individual objects and composites of objects.
  • To treat all objects in a composite uniformly.