[C++] Design Pattern - Builder

builder pattern

First of all, let’s have a general understanding of what the builder pattern means and how it is implemented?

  • First of all, the builder pattern is a creative design pattern
  • Another one is to use multiple simple objects to build a complex object step by step.
  • It can separate the construction of a complex object and provide the best way to create objects.

Builder pattern is mainly used to build complex problems

concept

Builder pattern is one of the object creation patterns that is used to hide the creation process of composite objects. It abstracts the creation process of composite objects and dynamically creates objects with composite properties through subclass inheritance and overloading. .

Simulating the Builder Pattern

The builder pattern is mainly implemented based on four core classes:

  • Abstract product category:
  • Specific product category: a specific product object category
  • Abstract Builder class: abstract interface for each component required to create a product object
  • Builder class of specific products: implement abstract interfaces and build various components
  • Director Director class: Unifies the construction process, provides it to the caller, and constructs products through the director

mind Mapping

Insert image description here

Code
#include <iostream>
#include <string>
#include <memory>

//构建电脑需要的零件
class Computer
{
    
    

public:
    Computer(){
    
    }

    void setBoard(const std::string &board)
    {
    
    
        _board = board;
    }
    void setDisplay(const std::string &display)
    {
    
    
        _display = display;
    }

    void showParamaters()//最终终端输出
    {
    
    
        std::string param = "Computer Paramaters:\n";
        param += "\tBoard:"+_board + "\n";
        param += "\tDisplay:"+_display + "\n";
        param += "\tOs:"+_os + "\n";
        std::cout << param << std::endl;
    }

    virtual void setOs() = 0;//纯虚函数

protected:
    std::string _board;//主板
    std::string _display;//显示器
    std::string _os;//操作系统
};

class MacBook :public Computer
{
    
    
public:

    void setOs()override
    {
    
    
        _os = "Mac OS X12";
    }
};

class Builder
{
    
    
public:
    virtual void buildBoard(const std::string &board) = 0;//纯虚函数
    virtual void buildDisplay(const std::string &display) = 0;
    virtual void buildOs() = 0;
    virtual std::shared_ptr<Computer> build() = 0;//定义了一个Computer智能指针对象
};

class MacBookBuilder : public Builder
{
    
    
public:
    MacBookBuilder():_computer(new MacBook()){
    
    }
    void buildBoard(const std::string& board)
    {
    
    
        _computer->setBoard(board);
    }

    void buildDisplay(const std::string& display)
    {
    
    
        _computer->setDisplay(display);
    }

    void buildOs()
    {
    
    
        _computer->setOs();
    }

    std::shared_ptr<Computer> build()
    {
    
    
        return _computer;
    }
private:
    std::shared_ptr<Computer> _computer;
};

//指导者
class Director
{
    
    
public:
    Director(Builder* builder):_builder(builder){
    
    }
    void construct(const std::string &board,const std::string &display)
    {
    
    
        _builder->buildBoard(board);
        _builder->buildDisplay(display);
        _builder->buildOs();
    }
private:
    std::shared_ptr<Builder> _builder;
};


int main()
{
    
    
    Builder *builder = new MacBookBuilder();
    std::unique_ptr<Director> director(new Director(builder));
    director->construct("华硕主板","三星显示器");

    std::shared_ptr<Computer> computer = builder->build();
    computer->showParamaters();
    return 0;

}

Insert image description here

Guess you like

Origin blog.csdn.net/wh9109/article/details/133527463