Javatpoint Logo
Javatpoint Logo

Composite Design Pattern in C++

The compositive design pattern in C++ is a structural program, which treats the group of objects and individual objects uniformly. These patterns are particularly useful when dealing with the part-whole hierarchies, where clients need to interact with both individual elements and complex structures in a consistent manner. In C++, the composite pattern promotes code flexibility, making it easier to add new types of components, alter the hierarchy structure, and perform recursive operations on the composite structure.

The key components of the composite pattern include the "component", which is the common interface for all concrete classes. Whether they are leaf nodes or composite nodes. The "leaf" will represent the end objects with no child elements. The "composite" which can obtain both leaf and other composite objects, forming a recursive tree structure.

These patterns are used when clients ignore the difference between the 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.

Simple example to understand the composite design pattern:

Imagine a situation where you have to buy the groceries from the market so you have prepared the list of items. The list can be of two types that are individual items and categories of items.

Categories of items represent the composite. Example fruits which further contains the apples, bananas, oranges. Snacks which contain chips, biscuits, cookies etc.

Here the eggs, milk, bread are leaf components they are basic building blocks and do not contain other items, where as categories like fruits, snacks are composite components, because they contain the Individual items and other categories. Just like the apples in fruits category.

When you go out for shopping you will treat both individual items and categories uniformly. You don't need to distinguish between picking up a single item and picking up a category because both provide the same interface here it is buying item.

Main components of the Composite Design Pattern:

Component: It is the abstract base class. It declares the common interface for all concreate classes. It contains pure virtual functions.

Leaf: It will represent the individual objects that do not have any children. It inherits the component class and provides a concrete implementation of its interface.

Composite: It represents the composite of objects, including both leaf and other composite objects. It inherits the component class and has a container to store child components. It implements the methods to manipulate children. It implement all component methods.

Client: It uses the component interface to work with both leaf and composite objects uniformly. It will interact with the components without knowing whether they are leaf or composite.

Real life situations where Composite Design Patterns are used:

Graphic systems:

In graphic design applications, shapes, lines and other graphical elements can be combined to create complex drawings. This pattern allows the uniform treatment of individual graphical elements and compositions of elements.

User interfaces:

These interfaces have hierarchical structure with components like buttons, panels and windows. This composite design pattern will treat most of the UI components and composite components uniformly.

File systems:

The files are also stored in the directories as tree-like structure. So this system will helpful for representing and interacting with this hierarchical file system structure consistently.

Document Object Model:

In web development, the DOM represents the HTML documents. This patterns is applied to represent HTML elements, where a complex document can be seen as a composition of individual elements like paragraphs, images and headings.

Menu Systems:

Menu systems in applications often have a hierarchical structure with menus containing sub-menus and menu items. The Composite Pattern allows the representation of menu structures in a way that is easy to navigate and manipulate.

CAD software:

Drawing applications and computer-Aided Design software utilize the composite pattern to manage complex structures of shapes, lines and other drawing elements.

Database Design:

Databases often contain hierarchical data structure, such as employee hierarchies, product categories, or files system. So these patterns are used for implementing recursive queries, encapsulating data access logic etc.

Program to illustrate the Composite Design Pattern:

Let's consider a practical example of using the composite design pattern in a drawing application. Here, we want to represent shapes such as circles, rectangles and triangle in a way that allows us to treat individual shapes and composite drawings uniformly.


Composite Design Pattern in C++


The program will clearly explain about the composite design pattern. In the program the component is shape class. It has no variables and it has one pure virtual function representing the common interface for drawing the shapes.

After that, the leaf components are Rectangle class, Circle class, Triangle class. These classes have no variables. The function draw () is for concreate implementation of the draw function specific to each shape.

The Drawing class will represent the composite in composite design pattern. It has one variable that is a vector with shape type. It is a container to hold leaf and composite shapes. It contains two functions one is addShape() and other is the draw() function. addShape() will take the pointer to a shape either leaf or another composite. It returns type is void. It adds a shape to the shapes vector of the drawing. For the draw function there is no parameters. And its return type is also void. This function will override the draw function from the Shape base class. It iterates through the shapes vector and calls the draw function for each shape, effectively drawing the entire composite structure.

The control flow of the Program is as follows:

  • Create Individual Shapes (Circle, Rectangle, Triangle):

Individual shapes are instantiated.

  • Create Composite Drawing (compositeDrawing):

A composite drawing object is instantiated.

  • Add Shapes to Composite Drawing (compositeDrawing):

The Circle and Rectangle objects are added to the compositeDrawing.

  • Create Nested Drawing (nestedDrawing):

Another drawing object (nestedDrawing) is instantiated.

  • Add Nested Drawing to Composite Drawing (compositeDrawing):

The nestedDrawing (which contains a Triangle) is added to the compositeDrawing.

  • Draw Individual Shapes and Composite Drawing:

The draw function is invoked for each individual shape (Circle, Rectangle, Triangle).

The draw function is invoked for the compositeDrawing, which in turn calls the draw functions for the individual shapes it contains.

The program demonstrates the Composite Design Pattern, where shapes are treated uniformly through the common interface (Shape). Individual shapes and composite drawings are created, and the composite drawing contains a nested drawing. After that, the draw function is called to visualize both individual shapes and the composite structure. This pattern allows for the seamless integration of leaf and composite objects, providing a unified way to work with complex structures of objects.

Advantages of Composite design pattern:

There are several advantages of the composite design pattern in C++. Some main advantages of the composite design pattern in C++ are as follows:

Unified Interface:

The pattern provides a unified interface for both individual objects and compositions. Clients can interact with objects uniformly, regardless of whether they are leaf nodes or composite nodes.

Flexibility and Extensibility:

The pattern makes it easy to add new types of components to the system. Whether it's a new leaf or a new composite, the client code remains unchanged. Introducing new shapes, elements, or categories becomes a seamless process, promoting system flexibility.

Code Reusability:

Components, whether they are leaves or composites, share a common interface. It promotes code reuse, as the same client code can be used for various elements within the hierarchy. Reusable code simplifies maintenance and reduces the likelihood of introducing errors.

Recursive Operations:

The composite structure supports recursive operations on the entire hierarchy. It is particularly useful for tasks that involve traversing and manipulating complex structures of objects. Operations applied to a composite node are automatically applied to its children, simplifying the implementation of algorithms that need to process the entire structure.

Hierarchical Structures:

The pattern is well-suited for representing and working with hierarchical structures, such as part-whole relationships, in a consistent manner.

Consistent Client Code:

Clients interact with the composite structure consistently, leading to cleaner and more intuitive code. There's no need for conditional statements to handle different types of objects, as the common interface abstracts away the implementation details.


The composite pattern encapsulates the complexity of managing individual objects and compositions within the composite nodes.


The Composite Design Pattern in C++ provides a powerful and flexible way to structure code for part-whole hierarchies. By unifying the interface for individual objects and compositions, it simplifies client interactions and enhances code flexibility. Its key components - Component, Leaf, Composite, and Client - work together seamlessly, promoting code reuse and extensibility. Real-world applications, from graphic systems to file systems, benefit from its ability to handle hierarchical structures uniformly. The provided example with grocery items illustrates the pattern's simplicity and effectiveness in practical scenarios. In conclusion, the Composite Pattern is a valuable tool for creating scalable, maintainable, and consistent software architectures.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA