GoF describes the Composite Design Pattern as “Compose objects into a tree structure to represent part-whole hierarchies. Composite lets the client treat individual objects and compositions of objects uniformly”. This seems over-complicated to me. So, I would not go into tree-leaf kind of jargon. Rather, I directly saw 2 or 3 different ways to implement Composite Design Pattern in Modern C++. But in simple words, the Composite Design Pattern is a Structural Design Pattern with a goal to treat the group of objects in the same manner as a single object.

By the way, If you haven’t checked out my other articles on Structural Design Patterns, then here is the list:

  1. Adapter
  2. Bridge
  3. Composite
  4. Decorator
  5. Facade
  6. Flyweight
  7. Proxy

The code snippets you see throughout this series of articles are simplified, not sophisticated. So you often see me not using keywords like overridefinalpublic(while inheritance) just to make code compact and consumable (most of the time) in single standard screen size. I also prefer struct instead of class just to save line by not writing “public:” sometimes and also miss virtual destructor, constructor, copy constructor, prefix std::, deleting dynamic memory, intentionally. I also consider myself a pragmatic person who wants to convey an idea in the simplest way possible rather than the standard way or using Jargons.

**_Note: _**If you stumbled here directly, then I would suggest you go through What is design pattern? first, even if it is trivial. I believe it will encourage you to explore more on this topic.

All of this code you encounter in this series of articles are compiled using C++20(though I have used Modern C++ features up to C++17 in most cases). So if you don’t have access to the latest compiler you can use https://wandbox.org/ which has preinstalled boost library as well.

Intent

To treat individual & group of objects in a uniform manner.

Objects in factory cartoon

So what is it all about and why do we need it. Well, we know that objects typically use other objects fields or properties or members through either inheritance or composition.

For example, in drawing applications, you have a Shape(e.g. Circle) that you can draw on the screen but you can also have a group of Shapes(e.g. vector<Circle>) which inherits from a collection Shape.

And they have certain common API which you can then call on one or the other without knowing in advance whether you’re working with a single element or with the entire collection.

Composite Design Pattern Examples in C++

So if you think about an application such as PowerPoint or any kind of vector drawing application you know that you can draw & drag individual shapes around.

But you can also group shapes together. And when you group several shapes together you can treat them as if they were a single shape. So you can grab the entire thing and also drag it and resize it and whatnot.

So, we’re going to implement the Composite Design Pattern around this idea of several different shapes.

#tutorial #cpp #design pattens #cpp11 #composite design pattern #c++

Composite Design Pattern in Modern C++
7.70 GEEK