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

Comments

  1. This blog has amazing information regarding this topic. The posts and blogs are very unique. Thank you so much for sharing the details. Used Lincoln Engine

    ReplyDelete
  2. Wonderful Post. This is a very helpful post.If any one who want to learn C&C++ get a free demo call on 9311002620 or visit our further websites :-https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete
  3. Thanks For Your valuable posting, it was very informative. this is wonderful post enjoyed reading this post.very helpful. Used Land Rover Engine

    ReplyDelete
  4. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.Best Alternative To Tumblr

    ReplyDelete
  5. Wow, yeah it's a very good Blogs for you Query.Other wise If If Any one who want learn C++ & Advance contact us on 9311002620 or visit :- https://htsindia.com/Courses/embedded-systems-and-robotics/cplusplustraininginstituteinsouthdelhi

    ReplyDelete
  6. Very well written article. It was an awesome article to read. Complete rich content and fully informative. I totally Loved it.website builder Ireland

    ReplyDelete

Post a comment