C++ Builder Pattern Example

The definition of the Builder Pattern is a separation of the construction of a complex object from its representation.

The builder pattern allows you to enforce a step-by-step process to construct a complex object as a finished product. In this pattern, the step-by-step construction process remains the same but the finished products can have different representations.

Builder Pattern Example in C++

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

class Wheel{
    public:
        int size;
};

class Engine{
    public:
        int horsepower;
};

class Body{
    public:
        string shape;
};

class Car{
    public:
        Wheel* wheels[4];
        Engine* engine;
        Body* body;

        void specifications(){
            cout<<"Body: "<<body->shape<<endl;
            cout<<"Engine Horsepower: "<<engine->horsepower<<endl;
            cout<<"Tire Size: "<<wheels[0]->size<<endl;
        }
};

class Builder{
    public:
        virtual Wheel* getWheel() = 0;
        virtual Engine* getEngine() = 0;
        virtual Body* getBody() = 0;
};


class JeepBuilder : public Builder{
    public:
        Wheel* getWheel(){
            Wheel* wheel = new Wheel();
            wheel->size = 22;
            return wheel;
        }    
        Engine* getEngine(){
            Engine* engine = new Engine();
            engine->horsepower = 400;
            return engine;
        }
        Body* getBody(){
            Body* body = new Body();
            body->shape = "SUV";
            return body;
        }
};

class NissanBuilder : public Builder {
    public:
        Wheel* getWheel(){
            Wheel* wheel = new Wheel();
            wheel->size = 16;
            return wheel;
        }

        Engine* getEngine(){
            Engine* engine = new Engine();
            engine->horsepower = 85;
            return engine;
        }
        Body* getBody(){
            Body* body = new Body();
            body->shape = "hatchback";
            return body;
        }
};

class Director {
    Builder* builder;
    public:
        void setBuilder(Builder* newBuilder){
            builder = newBuilder;
        }

        Car* getCar(){
            Car* car = new Car();

            car->body = builder->getBody();

            car->engine = builder->getEngine();

            car->wheels[0] = builder->getWheel();
            car->wheels[1] = builder->getWheel();
            car->wheels[2] = builder->getWheel();
            car->wheels[3] = builder->getWheel();

            return car;
        }
};

int main(){
    Car* car;
    Director director;
    JeepBuilder jeepBuilder;
    NissanBuilder nissanBuilder;

    cout<<"Jeep"<<endl;
    director.setBuilder(&jeepBuilder);
    car = director.getCar();
    car->specifications();

    cout<<endl;

    cout<<"Nissan"<<endl;
    director.setBuilder(&nissanBuilder);
    car = director.getCar();
    car->specifications();
    return 0;
}

Output

Jeep
Body: SUV
Engine Horsepower: 400
Tire Size: 22

Nissan
Body: hatchback
Engine Horsepower: 85
Tire Size: 16

Advantages of Builder Pattern

The main advantages of the Builder Pattern are as follows:

  • It provides a clear separation between the construction and representation of an object.
  • It provides better control over the construction process.
  • It supports to change the internal representation of objects.

Related C++ Design Patterns

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course