Composite Pattern - Object Tree Pattern
  • Composite is a structural design pattern that composes objects into tree structures to represent part-whole hierarchies
  • Composite lets clients treat individual objects and compositions of objects uniformly

Real-World Example

Code Example

What problems can the Composite design pattern solve?

  • A part-whole hierarchy should be represented so that clients can treat part and whole objects uniformly.
  • A part-whole hierarchy should be represented as a tree structure.

When defining Part objects and Whole objects that act as containers for Part objects, clients must treat them separately, which complicates client code.

Composite should be used when clients ignore the difference between compositions of objects and individual objects. If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous

What solution does the Composite design pattern describe?

  • Define a unified Component interface for both part Leaf objects and whole Composite objects.
  • Individual Leaf objects implement the Component interface directly, and Composite objects forward requests to their child components.

This enables clients to work through the Component interface to treat Leaf and Composite objects uniformly: Leaf objects perform a request directly, and Composite objects forward the request to their child components recursively downwards the tree structure. This makes client classes easier to implement, change, test, and reuse

Class Diagram

composite-pattern.drawio

Resources