Search⌘ K
AI Features

Composite Design Pattern

Explore the Composite design pattern by understanding its role in structuring part-whole hierarchies in software. Learn how this pattern enables uniform interaction with individual and composite objects through a common interface. This lesson helps you grasp its applicability, participants, and related design patterns to improve software architecture designs in C++.

Overview

Let’s take a top-down approach to understanding this pattern by starting with a definition. According to the Gang of Four book, “The composite design pattern composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.”

Before going deeper into the definition, let’s discuss what is meant by individual objects and the composition of objects.

The composition of objects has child components, and leaf objects don’t have any child components.

For example, let’s say we have a computer system with directories and files. We can consider directories as a composition of objects because we can find files or directories or both of them inside directories. On the other hand, ...