The Composite Design Pattern
The Composite pattern composes objects into tree structures to represent part-whole hierarchies.
Use the Composite pattern when you want to treat both individual objects and composites of objects uniformly. This pattern is useful for representing part-whole hierarchies.
- TypeStructural
- Time ComplexityO(n) Linear Time – The time taken grows linearly with the size of the input. Examples include simple search algorithms.
- Efficiency6
-
Learning Effort
- Average5
- Conceptual5
- Debug and Maintain5
- Implementation4
- Prerequisites4
- Versatility6
Things you should know before you start
- Recursion Understanding of recursion, as the pattern often uses recursive calls to operate on child components.
- Inheritance Understanding of inheritance and polymorphism in object-oriented programming.
Minimal Example
Representing a file system with folders and files.
interface FileSystemComponent { void show(); } class Folder implements FileSystemComponent { Listchildren; void show() { children.forEach(child => child.show()); } } const root = new Folder(); const file1 = new File(); const folder1 = new Folder(); root.add(file1); root.add(folder1); root.show();
What’s happening in thie example
In this example, the FileSystemComponent interface is implemented by both File and Folder classes. The `clientCode` constructs a tree structure and then calls `show` to display all elements.
How the Composite Works
The Composite pattern allows you to compose objects into tree structures to represent part-whole hierarchies. This makes it easier to work with the objects in the composition uniformly.
The pattern is particularly useful in graphics systems, tree structures like directories, and organizational hierarchies.
Entities in the Composite Design Pattern
-
ComponentDeclares the interface for objects in the composition and for accessing and managing its child components.
- Component has a one-to-many contains relationship with Leaf
- Composite has a one-to-many contains relationship with Component
What to watch out for when using or considering this design pattern.
Avoid using the Composite pattern when you don’t have a clear part-whole hierarchy, as it could make the design overly general. It may also complicate the design when you have to enforce restrictions on the hierarchy.
Design patterns often confused with the Composite Design Pattern
The following design patterns are often confused with the Composite
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Decorator – The Decorator pattern adds new functionalities to objects without altering their structure.
The Decorator pattern is similar to the Composite in the following ways.
- Both are structural design patterns.
- Both deal with object compositions.
- Composite focuses on a hierarchical tree structure, while Decorator focuses on adding new functionality to objects.
- Composite treats individual objects and compositions of objects uniformly, whereas Decorator focuses on enhancing objects.
-
Bridge – The Bridge pattern decouples an abstraction from its implementation so that the two can vary independently.
The Bridge pattern is similar to the Composite in the following ways.
- Both are structural design patterns.
- Both aim to simplify complex systems.
- Composite is used for treating individual and composite objects uniformly, while Bridge is used for separating an object’s abstraction from its implementation.
- Composite works well for tree-like structures, whereas Bridge is more about decoupling components.