Design mode (5) Builder mode

Builder mode

The builder mode separates the client from the creation of complex objects that contain multiple components. The client does not need to know the internal composition and methods of the complex object, but only needs to know the required builder.
To put it another way, when building the house, the developer gave the design drawing of the house he hoped to build to the construction party. The construction party lays the foundation, builds the house, and performs interior decoration according to the construction process designed by the company. The developer only You only need to contact a qualified construction party, and leave the rest to the construction party for construction. The construction party will build houses of different styles according to the developer's design drawings.

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

Builder mode structure

The structure of the builder mode includes the following roles:

  1. Abstract Builder (AbstractBuilder) : Create an abstract interface specified by each component of a Product object;
  2. Concrete Builder (ConcreteBuilder) : implements the AbstractBuilder interface, implements the concrete construction method and assembly method of each component, and returns the creation result.
  3. Product : specific product object
  4. Director : Construct an object using the Builder interface and arrange the construction of complex objects. The client generally only needs to interact with the Director, specify the builder type, and then pass the concrete builder object to the Director through the constructor or setter method . Its main function is to isolate the production process of customers and objects, and be responsible for controlling the production process of product objects.

UML diagram:
Insert picture description here
Now look at the UML diagram. The Director selects the appropriate Builder according to the needs, and then the Builder will build its corresponding Product. Director does not need to know how Builder implements the production process of Product.

Builder mode code example

BuilderPattern.h

#include <iostream>
#include <string.h>

using namespace std;

//产品类House
class House
{
    
    
public:
  House() {
    
    }

  void setFloor(string iFloor)
  {
    
    
    this->floor = iFloor;
  }

  void setWall(string iWall)
  {
    
    
    this->wall = iWall;
  }

  void setRoof(string iRoof)
  {
    
    
    this->roof = iRoof;
  }

  //打印House信息
  void printfHouseInfo()
  {
    
    
    cout << "Floor " << this->floor.c_str() << "\n";
    cout << "Wall " << this->wall.c_str() << "\n";
    cout << "Roof " << this->roof.c_str() << "\n\n";
  }

private:
  string floor;
  string wall;
  string roof;
};

//抽象建造者AbstractBuilder
class AbstractBuilder
{
    
    
public:
  AbstractBuilder()
  {
    
    
    house = new House();
  }

  virtual ~AbstractBuilder()
  {
    
    
    if (house != nullptr)
    {
    
    
      house = nullptr;
      delete house;
    }
  }

  //抽象方法:
  virtual void buildFloor() = 0;
  virtual void buildWall() = 0;
  virtual void buildRoof() = 0;
  virtual House *getHouse() = 0;

  House *house;
};

//具体建造者ConcreteBuilderA
class ConcreteBuilderA : public AbstractBuilder
{
    
    
public:
  ConcreteBuilderA()
  {
    
    
    printf("ConcreteBuilderA\n");
  }

  ~ConcreteBuilderA()
  {
    
    
    if (this->house != nullptr)
    {
    
    
      house = nullptr;
      delete house;
    }
  }

  //具体实现方法
  void buildFloor()
  {
    
    
    this->house->setFloor("Floor_A");
  }

  void buildWall()
  {
    
    
    this->house->setWall("Wall_A");
  }

  void buildRoof()
  {
    
    
    this->house->setRoof("Roof_A");
  }

  House *getHouse()
  {
    
    
    return this->house;
  }
};

//具体建造者ConcreteBuilderB
class ConcreteBuilderB : public AbstractBuilder
{
    
    
public:
  ConcreteBuilderB()
  {
    
    
    printf("ConcreteBuilderB\n");
  }

  ~ConcreteBuilderB()
  {
    
    
    if (this->house != nullptr)
    {
    
    
      house = nullptr;
      delete house;
    }
  }

  //具体实现方法
  void buildFloor()
  {
    
    
    this->house->setFloor("Floor_B");
  }

  void buildWall()
  {
    
    
    this->house->setWall("Wall_B");
  }

  void buildRoof()
  {
    
    
    this->house->setRoof("Roof_B");
  }

  House *getHouse()
  {
    
    
    return this->house;
  }
};

//指挥者Director
class Director
{
    
    
public:
  Director() : builder(nullptr) {
    
    }

  ~Director()
  {
    
    
    if (this->builder != nullptr)
    {
    
    
      builder = nullptr;
      delete builder;
    }
  }
  //具体实现方法
  void setBuilder(AbstractBuilder *iBuilder)
  {
    
    
    this->builder = iBuilder;
  }

  House *construct()
  {
    
    
    builder->buildFloor();
    builder->buildWall();
    builder->buildRoof();
    return builder->getHouse();
  }

private:
  AbstractBuilder *builder;
};

BuilderPattern.cpp

#include "BuilderPattern.h"

int main()
{
    
    
  // 抽象建造者 builder
	AbstractBuilder *builder;
  // 装饰者
	Director *director = new Director();
  // 产品类:house
	House *house;

  // 新建具体建造者A
	builder = new ConcreteBuilderA();
	director->setBuilder(builder);
	house = director->construct();
	house->printfHouseInfo();
	delete builder;

  // 新建具体建造者B
	builder = new ConcreteBuilderB();
	director->setBuilder(builder);
	house = director->construct();
	house->printfHouseInfo();

	director->~Director();
	builder->~AbstractBuilder();

	return 0;
}

Summary of builder mode

It can be seen from the client that the customer specifies the corresponding builder as needed, and passes the builder as a parameter to the commander, and the result can be obtained through the commander. The client does not need to care about the specific construction process of the product.
If you want to change the construction style, you only need to change the specific builder. There is no connection between different builders, which is convenient for replacement. From the point of view of code optimization, in fact, the role of the director is not needed, and the construct method can be directly put into the concrete builder.

advantage:

  • In the builder mode, the client does not need to know the internal composition details of the product, and separates the product itself from the product creation process, so that the same creation process can create different product objects;
  • Different builders are independent of each other, and there is no chain, which is convenient for replacement.

Disadvantages:

  • The products created by the builder mode generally have more in common, and their components are similar. If the differences between the products are large, the builder mode is not suitable for use, so the scope of its use is subject to certain restrictions.
  • If the internal changes of the product are complex, it may lead to the need to define a lot of specific builder classes to achieve this change, resulting in the system becoming very large

Applicable environment:

  • The product object that needs to be generated has a complex internal structure (usually containing multiple member variables);
  • The internal attributes of the product object have a certain generation sequence;
  • The same creation process applies to many different products.

Guess you like

Origin blog.csdn.net/qq_24649627/article/details/115176069