Builder Design pattern Description
Builder Design pattern Example Source code in c++:
/* * File:builder.cpp * This file is described the builder Design Pattern with help of example * Author: Aplha * Date: 29 Dec 2020 */ //Header File #include<iostream> //Engine class Engine { int m_Power; public: void setPower(int pw) { m_Power = pw; } int getPower(){ return m_Power;} }; //Body class Body { int m_BodySize; public: void setBody(int sz) { m_BodySize = sz; } int getBody() { return m_BodySize;} }; //Wheel class Wheel { int m_WheelSize; public: void setWheel(int sz) { m_WheelSize = sz; } int getWheel() { return m_WheelSize;} }; //Car Class class Car { //Data member MUST BE PRIVATE but here I am using as public for demo example public: Engine m_eng; Body m_body; Wheel m_wh; public: }; //HyundaiCarBuilder class IHyundaiCarBuilder { protected: Car* m_car; public: virtual void BuildEngine() = 0; virtual void BuildBody() = 0; virtual void BuildWheel() = 0; virtual Car GetCar() = 0; }; //Car_i10Builder class Car_i10Builder: public IHyundaiCarBuilder { public: Car_i10Builder() { m_car = new Car; } ~Car_i10Builder() { delete m_car;} void BuildEngine(){ m_car->m_eng.setPower(1000);} void BuildBody(){ m_car->m_body.setBody(100);} void BuildWheel(){m_car->m_wh.setWheel(10);} Car GetCar() { std::cout<<"Car i10 is ready"<<std::endl; return (*m_car); } }; //Car_i20Builder class Car_i20Builder: public IHyundaiCarBuilder { public: Car_i20Builder() { m_car = new Car; } ~Car_i20Builder() { delete m_car;} void BuildEngine(){ m_car->m_eng.setPower(2000);} void BuildBody(){ m_car->m_body.setBody(200);} void BuildWheel(){m_car->m_wh.setWheel(20);} Car GetCar() { std::cout<<"Car i20 is ready"<<std::endl; return (*m_car); } }; //Director class Director { IHyundaiCarBuilder* m_builder; public: Director(IHyundaiCarBuilder* build): m_builder(build) {} void SetBuilder(IHyundaiCarBuilder* build) { m_builder = build; } void Construct() { m_builder->BuildEngine(); m_builder->BuildBody(); m_builder->BuildWheel(); } }; //Client int main() { std::cout<<"Builder Design Pattern"<<std::endl; std::cout<<"Using i10 Builder for building i10"<<std::endl; IHyundaiCarBuilder* build = new Car_i10Builder; Director* dir = new Director(build); dir->Construct(); Car car = build->GetCar(); std::cout<<"Engine Power:"<<car.m_eng.getPower()<<std::endl; std::cout<<"Body Size:"<<car.m_body.getBody()<<std::endl; std::cout<<"Wheel Size:"<<car.m_wh.getWheel()<<std::endl; delete build; std::cout<<"Using i20 Builder for building i20"<<std::endl; IHyundaiCarBuilder* buildi20 = new Car_i20Builder; dir->SetBuilder(buildi20); dir->Construct(); car = build->GetCar(); std::cout<<"Engine Power:"<<car.m_eng.getPower()<<std::endl; std::cout<<"Body Size:"<<car.m_body.getBody()<<std::endl; std::cout<<"Wheel Size:"<<car.m_wh.getWheel()<<std::endl; delete buildi20; delete dir; return 0; }
Compilation command:
g++ -std=c++11 builder.cpp
Output:
Builder Design Pattern
Using i10 Builder for building i10
Car i10 is ready
Engine Power:1000
Body Size:100
Wheel Size:10
Using i20 Builder for building i20
Car i20 is ready
Engine Power:2000
Body Size:200
Wheel Size:20
Builder Design pattern Description