Chapter 13, the builder pattern

A concept

  • When you need to build its separation of presentation of a complex object, so that the same construction process can create different representations of intent, we need to apply to a design model, the builder pattern, known as the generator mode, the builder pattern can the internal representation of a product generation process and product separated, which can make the process of generating a construction product objects having different internal representation. If we use the builder pattern, then the user will only need to build the type you can get them, and process and details of the specific construction does not need to know.

  • Builder mode: Construction and its separation represents a complex object, such that the same build process can create different representations.

Two roles

  • Builder is to create a Product object of the various components of the development of abstract interface
  • ConcreteBuilder specific creator, to achieve the various components Builder interface constructed and arranged
  • Product specific product role
  • Director commander, he is to build an object using the Builder interface

Three C ++ code to achieve

//建造者模式
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

//产品类-由多个部件组成
class Product
{
public:
    void Add(string part)  //添加产品部件
    {
        parts.push_back(part);
    }
    void Show()  //列举所有的产品部件
    {
        cout << "\n产品 创建 --" << endl;
        for (vector<string>::iterator it = parts.begin(); it != parts.end(); ++it)
        {
            cout << *(it) << endl;
        }
    }
private:
    vector<string> parts;
};
//Builder类-抽象建造者类,确定产品由两个部件PartA和PartB组成
//并声明一个得到产品建造后结果的方法GetResult
class Builder
{
public:
    virtual void BuildPartA() = 0;
    virtual void BuildPartB() = 0;
    virtual Product* GetResult() = 0;
};

//具体建造者类
class ConcreteBuilder1 : public Builder
{
public:
    ConcreteBuilder1()
    {
        product = new Product;
    }
    ~ConcreteBuilder1()
    {
        delete product;
    }
    void BuildPartA() override
    {
        product->Add("部件A");
    }
    void BuildPartB() override
    {
        product->Add("部件B");
    }
    Product* GetResult() override
    {
        return product;
    }
private:
    Product* product;

};

//具体建造者类
class ConcreteBuilder2 : public Builder
{
public:
    ConcreteBuilder2()
    {
        product = new Product;
    }
    ~ConcreteBuilder2()
    {
        delete product;
    }
    void BuildPartA() override
    {
        product->Add("部件X");
    }
    void BuildPartB() override
    {
        product->Add("部件Y");
    }
    Product* GetResult() override
    {
        return product;
    }
private:
    Product* product;

};

//指挥者类
class Director
{
public:
    void Construct(Builder* builder)
    {
        builder->BuildPartA();
        builder->BuildPartB();
    }
};

//客户端
int main()
{
    Director* director = new Director;
    Builder* b1 = new ConcreteBuilder1;
    Builder* b2 = new ConcreteBuilder2;

    //指挥者用ConcreteBuilder1的方法来建造产品
    director->Construct(b1);
    Product* p1 = b1->GetResult();
    p1->Show();
    //指挥者用ConcreteBuilder2的方法来建造产品
    director->Construct(b2);
    Product* p2 = b2->GetResult();
    p2->Show();
    
    system("pause");

    return 0;
}

Shenkaoziliao
1 "Westward Design Patterns in C ++ - Chapter 13 - the builder pattern" https://blog.csdn.net/xiqingnian/article/details/42005627
2 "Westward design mode" with Cheng Jie

Guess you like

Origin www.cnblogs.com/Manual-Linux/p/11121394.html