Composite design pattern in C++ with real-life examples

The composite design patterns is a part of the structural design pattern s which is used when you are creating a system or software in a tree structure

We can also say that it is easy to implement a tree structure with a composite design pattern

A real-life example

this time, suppose you are making a software application called a school bag and the application is implemented using a tree structure which is represented in the below object structure diagram

on the whole, the application school bag contains three sections

in the first place, if we open section 1 we can draw a water bottle and section 3

in the second place, if we open section 3 we can draw the Book and lunch box

in the third place, if we open section 2 then we can draw colour pencils and a drawing book

until now, the software application called school contains  4 composite objects and  5 leaf objects

in reality, Composite: composite object can have multiple objects

in general, Leaf: leaf object should not have objects

Object structure

Rendered by QuickLaTeX.com

Structure

Rendered by QuickLaTeX.com

C++ example

to clarify, in the below example I have used list data structure  you can use any data structure like arrays, hash tables, trees  

#include <iostream>
#include <list>
using namespace std;

class School_bag
{
public:
    virtual void draw() = 0;
};
class Water_bottle : public School_bag
{
public:
    void draw(){
        cout << "Water bottle" << endl;
    }
};
class Colour_pencils : public School_bag
{
public:
    void draw(){
        cout << "Colour_pencils" << endl;
    }
};
class Lunch_box : public School_bag
{
public:
    void draw(){
        cout << "Lunch_box" << endl;
    }
};
class Drawing_book : public School_bag
{
public:
    void draw(){
        cout << "Drawing_book" << endl;
    }
};
class Book: public School_bag
{
public:
    void draw(){
        cout << "Books" << endl;
    }
};
class CompositeSchool_bag : public School_bag
{
private:
    list<School_bag *> child_Schoolbag;
public:
    void draw()
    {
        for(list<School_bag *>::iterator Schoolbag  = child_Schoolbag.begin();  Schoolbag != child_Schoolbag.end(); ++Schoolbag){
            (*Schoolbag)->draw();
        }
    }
    void add(School_bag *Schoolbag)
    {
        child_Schoolbag.push_back(Schoolbag);
    }
    void remove(School_bag *Schoolbag)
    {
		child_Schoolbag.remove(Schoolbag);
    }
};
int main()
{
    Water_bottle *Water_bottle1 = new Water_bottle();
	 Colour_pencils*Colour_pencils1 = new Colour_pencils();
	 Lunch_box*Lunch_box1=new Lunch_box();
	 Book *Book1= new Book();
	Drawing_book *Drawing_book1 = new Drawing_book();
	CompositeSchool_bag *Schoolbag = new CompositeSchool_bag();
	CompositeSchool_bag *Section1 = new CompositeSchool_bag();
	CompositeSchool_bag *Section2= new CompositeSchool_bag();
	CompositeSchool_bag *Section3= new CompositeSchool_bag();
	Schoolbag->add(Section1);
	Schoolbag->add(Section2);
	Section1->add(Water_bottle1);
	Section1->add(Section3);
	Section2->add(Colour_pencils1);
	Section2->add(Drawing_book1);
	Section3->add(Lunch_box1);
	Section3->add(Book1);
	Schoolbag->draw();
    return 0;
}

Output

Water bottle
Lunch_box
Book
Colour pencils
Drawing_book

You can use the same operation on leaf and composite but if it is leaf object it will only print the value of the leaf

For example, the leaf object Book1 will print only the value of the leaf

Book1->draw();

Suppose if it is a composite object then it will print all values of leaf object

Section1->draw();

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.