The builder design pattern in C++ with a example

As you might know, builder design pattern falls under creational design pattern which allows you to create complex object step by step so that you can represent the same object in different forms

To understand the builder design pattern consider a simple example suppose you have a plate object(builder), now you can serve this plate with rice, salad, chicken

Every time the plate will be the same but it will be served with different food varieties

Structure

Rendered by QuickLaTeX.com

Participants

  1. Director (Lenovo builder, HP builder, Dell builder)
  2. Builder  (laptop builder)
  3. Concrete builder (Director)
  4. Product (Lenovo, Hp, Dell)
#include <iostream>
using namespace std;
// product
class Laptop{
    string _Laptop;
    string _Operatingsystem;
    string _Batterylife;
public:
    Laptop(string LaptopBrand):_Laptop{LaptopBrand} {}
    void setBatterylife(string Brand)                 { _Batterylife = Brand; }
    void setOperatingsystem(string Operatingsystem)   { _Operatingsystem = Operatingsystem;   }
    string getBatterylife()     { return _Batterylife; }
    string getOperatingsystem() { return _Operatingsystem;   }
    void show() {
    	cout << "Laptop Brand: " <<_Laptop << endl
    		 << "Operatingsystem Type: " <<_Operatingsystem << endl
    		 << "Batterylife Type: "<<_Batterylife << endl << endl;
    }
};
// Builder(Abstract Class)
class LaptopBuilder{
protected:
    Laptop *_Laptop;
public:
    virtual void getPartsDone() = 0;
    virtual void buildOperatingsystem() = 0;
    virtual void buildBatterylife() = 0;
    Laptop* getLaptop(){ return _Laptop; }
};
// Concrete builder
class LenovoBuilder: public LaptopBuilder {
public:
    void getPartsDone() { _Laptop = new Laptop("Lenovo"); }
    void buildBatterylife()  { _Laptop->setBatterylife("4 hours Batterylife");   }
    void buildOperatingsystem()    { _Laptop->setOperatingsystem("Linux Operatingsystem");   }
};
// Concrete builder
class HPBuilder: public LaptopBuilder {
public:
    void getPartsDone() { _Laptop = new Laptop("HP "); }
    void buildBatterylife()  { _Laptop->setBatterylife("5 hours Batterylife");   }
    void buildOperatingsystem()    { _Laptop->setOperatingsystem("Mac os Operatingsystem");   }
};
// Concrete builder
class DellBuilder: public LaptopBuilder {
public:
    void getPartsDone() { _Laptop = new Laptop("Dell"); }
    void buildBatterylife()  { _Laptop->setBatterylife("6 hours Batterylife");   }
    void buildOperatingsystem()    { _Laptop->setOperatingsystem("Window 10 Operatingsystem");   }
};
//Director
// Defines steps and tells to the builder that build in given order.
class Director{
    LaptopBuilder *builder;
public:
    Laptop* createLaptop(LaptopBuilder *builder) {
        builder->getPartsDone();
        builder->buildOperatingsystem();
        builder->buildBatterylife();
        return builder->getLaptop();
    }
};
int main() {
    Director dir;
    HPBuilder hp;
    LenovoBuilder le;
    DellBuilder de;
	Laptop *_hp = dir.createLaptop(&hp);
	Laptop *len = dir.createLaptop(&le);
	Laptop *del = dir.createLaptop(&de);
	_hp->show();
	len->show();
	del->show();
    delete _hp;
    delete len;
    delete del;
	return 0;
}

Output

Laptop Brand:HP
Operating system:mac os
Battery life: 5hours
Laptop Brand:Lenovo 
Operating system:Linux
Battery life: 4hours
Laptop Brand: Dell 
Operating system: window 10
Battery life: 6hours

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.