Composite Design Pattern

The Composite Design Pattern is one of the Structural Design Patterns. It allows you to compose objects into tree structures to represent part-whole hierarchies. The key idea is that individual objects (leaves) and groups of objects (composites) are treated uniformly through a common interface.

Many real-world systems naturally form tree structures. Examples include file systems, company organizational charts, graphical user interface (GUI) layouts, and menu systems. Without the Composite pattern, client code would constantly need to check whether an object is a single item or a container before performing operations. The Composite pattern eliminates this complexity by defining a shared interface that both leaves and composites implement.

This document demonstrates two practical implementations of the Composite Design Pattern in C++.

Example 1: File System Directory Tree

This example models a simple file system consisting of individual files and folders. A folder can contain both files and other folders.

Program


Explanation

The IFileSystemComponent interface is shared by both File and Folder.

A File is a Leaf because it cannot contain child objects.

A Folder is a Composite because it stores other IFileSystemComponent objects, which may themselves be files or folders.

Since both implement the same interface, the client interacts with every object identically without checking its type.

Example 2: Company Organization Hierarchy

This example represents an organization where managers supervise employees as well as other managers.

Program

Explanation

The Employee class is a Leaf, representing an individual contributor.

The Manager class is a Composite because it stores a collection of IEmployee objects. These objects may be either employees or other managers.

The client treats every object through the IEmployee interface, allowing recursive traversal of the entire organizational hierarchy.


Characteristics of the Composite Design Pattern

PropertyDescription
Pattern TypeStructural Design Pattern
Core PrincipleRepresent part-whole hierarchies using a common interface for both individual objects and object groups.
Main ComponentsComponent, Leaf, Composite, and Client.
RelationshipThe Composite stores multiple Component objects, allowing recursive tree structures.
Primary BenefitClients interact uniformly with both single objects and groups of objects without explicit type checking.
Common ApplicationsFile systems, GUI component trees, menus, XML/HTML DOM trees, organization charts, and scene graphs.

Conclusion

The Composite Design Pattern simplifies working with recursive tree structures by allowing individual objects and collections of objects to share the same interface. This removes the need for complex type checks and enables recursive operations to be implemented naturally. As demonstrated in the File System and Company Organization Hierarchy examples, the Composite pattern produces cleaner, more scalable, and easier-to-maintain code whenever hierarchical structures are involved.