Composite | Cheat Sheet

Structural Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
4 min readJul 5, 2021

--

Composite is used for mapping part-whole hierarchies with object composition in tree structures. With this you have a consistent handling of individual objects as well as object compositions. Real-life examples are folder structures of operating systems or rank systems in military or companies.

An advantage of this pattern is that the client should not distinguish between individual objects and object compositions (Leaf / Composite) and that it facilitates the addition of new component types. Newly defined Composite or Leaf classes automatically work together with existing structures.

To reduce the memory size by sharing the abstract component, you can combine the Composite pattern with Singleton or Flyweight. To get an order for the child object, the pattern can be extended with an Iterator.

Real-life examples

  • Rank system in the military (general — officers — soldiers)
  • Operating systems folder structure
  • Managers who leads employees (Hierarchy structure)
  • Collection of things with same interface: animal collection, tree collection, or house collection
typical object structure

Meaning

  • Composition of objects in tree structures for mapping part-whole hierarchies
  • Consistent handling of individual objects as well as object compositions

Applicability (suitable if …)

  • The part-whole hierarchies of objects are supposed to be represented
  • The client should not distinguish between individual objects and object compositions (but uniform treatment)

Assets and Drawbacks

  • Defines class hierarchies consisting of compound objects
  • Client does not know whether it is an individual object (Leaf) or composite component (Component/Composite)
  • Facilitates the addition of new component types. Newly defined composite or Leaf classes automatically work together with existing structures
  • System design becomes universal as the structure cannot be bounded (only with runtime checks) as opposed to composition with specific components

Extensibility

  • Explicit references to parent objects: easier traversal and management of the structure through better navigation
  • Abstract component sharing: to reduce memory usage (combination with Singleton or Flyweight)
  • Order of child objects (e.g. front-to-back order) by Iterator
  • Use of caching for frequent traversal
  • Leaf object can also vary from the class instance

Example

For this example I was inspired by Refactoring Guru.

We use the analogy of a rank system in the military. The abstract base class is a soldier, who should salute() the superior.

abstract base class

A Composite interacts as a node for further Composites or Leafs. For the analogy, this is a general or an officer.

Composite that accepts or removes children

You can add or remove children that get called with the salute() function to salute.

Composite that calls its children

At last, there is a Leaf that reflects the soldier who only salutes the superior.

Leaf

The client can generate its own tree of Composite and Leaf children. The salute() function iterates through the structure.

client

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Structural Patterns are indispensable when objects are combined to create new functionalities. Comprehensive structures are built by using compositions of classes and objects. This provides flexibility due to variable composition structure depending on the runtime. One thus achieves an increase in efficiency and consistency.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links