Builder mode of design mode

Scenario:

We want a car in real life. We don't need to go to the factory and start production and assembly from the most basic parts. We just need to choose the model we like to buy, and we can get a complete car. In software development, developers also hope that building the system can be as convenient and simple as buying a car, so there is the builder model.

definition:

Creator pattern: separate the construction of a complex object from its representation, so that the same construction process can create different representations.

Builder mode is to create a complex object step by step. It allows users to build complex objects only by specifying their types and contents. Users do not need to know the specific internal construction details.

Structure:

The essence of builder mode is to encapsulate the construction of target products, and let users directly obtain the desired objects through a unified interface. It can be divided into the following categories:

  • Builder (Abstract builder): specify abstract interfaces for each part that creates a product object.

  • ConcreteBuilder (concrete Builder): implement the Builder interface to construct and assemble various parts of the product, define and clarify the representation it creates, and provide an interface to retrieve the product.

  • Director: construct an object that uses the Builder interface.

  • Product: represents a complex object being constructed. ConcreteBuilder creates the internal representation of the product and defines its assembly process, including the classes that define the components, including the interfaces that assemble these components into the final product.

realization:

This time we take the car object as an example to design a simple builder. Suppose there are Mercedes Benz and BMW models for us to choose.

Product:

class Car{
  public:
    void setWheel(const std::string& wheel) {m_wheel = wheel;}
    void setEngine(const std::string& engine) {m_engine = engine;}
    void setBrake(const std::string& brake) {m_brake = brake;}
    
    std::string getWheel() const {return m_wheel;}
    std::string getEngine() const {return m_engine;}
    std::string getBrake() const {return m_brake;}
    
  private:
    std::string m_wheel;
    std::string m_engine;
    std::string m_brake;
};

builder:

class Builder{
  public:
    virtual void BuildWheel() = 0;
    virtual void BuildEngine() = 0;
    virtual void BuildBrake() = 0;
    virtual Car* GetResult() = 0;
};

ConcreteBuilder:

class BenChiBuilder : public Builder{
  public:
    BenChiBUilder() {m_car = new Car();}
    void BuildWheel() {m_car->setWheel("BenChiWheel");}
    void BUildEngine() {m_car->setEngine("BenChiEngine");}
    void BuildBrake() {m_car->setBrake("BenChiBrake");}
    Car* GetResult() const {return m_car;}
  private:
    Car* m_car;
};

class BaoMaBuilder : public Builder{
  public:
    BaoMaBUilder(): m_car(new Car()){}
    void BuildWheel() {m_car->setWheel("BaoMaWheel");}
    void BUildEngine() {m_car->setEngine("BaoMaEngine");}
    void BuildBrake() {m_car->setBrake("BaoMaBrake");}
    Car* GetResult() const {return m_car;}
  private:
    Car* m_car;
};

Director:

class Director{
  public:
    Car* CreateCar(Builder* pBuilder){
        pBuilder->BuildWheel();
        pBuilder->BuildEngine();
        pBuilder->BuildBrake();
        return pBuilder->GetResult();
    }
};

main:

int main(){
    Director iDirector;
    Builder* pBuilder = new BaoMaBuilder();
    Car* myCar = iDirector.CreateCar(pBuilder);
    
    std::cout << myCar->getWheel() << std::endl
        	  << myCar->getEngine() << std::endl
        	  << myCar->getBrake() << std::endl;
    return 0;
}

Tags: C++ Algorithm

Posted on Fri, 17 Sep 2021 05:06:00 -0400 by w3evolutions