Detailed explanation of builder's design mode

Keywords: Windows

1, Definition

GOF definition: separate the construction of a complex object from its representation, so that the same construction process can create different representations. Such a design pattern is called builder pattern.
Builder pattern, also called builder pattern or generator pattern, is one of 23 design patterns proposed by GoF. Builder mode is one of the object creation modes, which 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 overload.
For example, computers are created by If the components such as OPU, mainboard, memory, hard disk, graphics card, case, display, keyboard, mouse are assembled, the purchaser can not assemble the computer by himself, but tell the computer sales company the configuration requirements of the computer. The computer sales company arranges the technical personnel to assemble the computer, and then hand them over to the purchaser who wants to buy the computer.
These products are all composed of multiple parts, each part can be selected flexibly, but the creation steps are the same, that is, the components of the product are unchanged, but each part can be selected flexibly. The creation of this kind of product cannot be described by the factory pattern described earlier. Only the builder pattern can describe the creation of this kind of product well.

2, Role

1. Builder:
Specifies an abstract interface for each part that creates a product object.
2. Concrete Builder:
The interface of Builder is implemented to construct and assemble various parts of the product, define and clarify the representation it creates, and provide an interface to retrieve the product.
3. Director:
Command and construct an object using the Builder interface.
4. Product:
Represents the complex object being constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including classes that define the components, including interfaces that assemble them into the final product.

ConcreteBuilder creates an internal representation of the product and defines its assembly process, including classes that define the components, including interfaces that assemble them into the final product.

The UML class diagram is as follows:

3, Implementation

#include<iostream>
using namespace std;

//Specific products, houses, by doors, windows, walls and other components.
class House{
private:
	string m_door;
	string m_window;
	string m_wall;
public:
	void setDoor(string door){
		this->m_door = door;
	}
	void setWindow(string window){
		this->m_window = window;
	}
	void setWall(string wall){
		this->m_wall = wall;
	}
	string getDoor(){
		return this->m_door;
	}
	string getDoor(){
		return this->m_window;
	}
	string getDoor(){
		return this->m_wall;
	}
};

//Builder, which provides interfaces for building various components of the house
class Builder{
public:
	virtual void builderDoor() = 0;
	virtual void builderWindow() = 0 ; 
	virtual void builderWall() = 0 ;
	virtual House* getHouse() = 0 ;
};

//The commander calls the specific builder to construct the specific product and directly interacts with the user, so that the user does not need to know the implementation details of the product.
class Director{
private:
	Builder *m_builder;
public:
	Director(Builder* builder){
		this->m_builder = builder;
	}
	void construct(){
		this->m_builder->builderDoor;
		this->m_builder->builderWindow;
		this->m_builder->builderWall;
	}
};

//Specific builder, set the parts of the object
//Building apartments
class FlatBuilder:public Builder{
private:
	House* m_house;
public:
	FlatBuilder(){
		this->m_house = new House;
	}
	void builderDoor(){
		this->m_house->setDoor("flat door");
	}
	void builderWindow(){
		this->m_house->setWindow("flat window");
	}
	void builderWall(){
		this->m_house->setWall("flat wall");
	}
	House* getHouse(){
		return this->m_house;
	}
};
//Building a villa
class VillaBuilder:public Builder{
private:
	House* m_house;
public:
	VillaBuilder(){
		this->m_house = new House;
	}
	void builderDoor(){
		this->m_house->setDoor("valli door");
	}
	void builderWindow(){
		this->m_house->setWindow("valli window");
	}
	void builderWall(){
		this->m_house->setWall("valli wall");
	}
	House* getHouse(){
		return this->m_house;
	}
};
int main(){

	House *house = nullptr;
	Builder *builder = nullptr;
	Director *director = nullptr;

	//The client gives the director the responsibility to build the house without knowing how to build it
	house = new House;
	builder = new FlatBuilder();
	director = new Director(builder);
	director->construct();
	delete director;
	delete builder;
	delete house;


	house = new House;
	builder = new VillaBuilder();
	director = new Director(builder);
	director->construct();
	delete director;
	delete builder;
	delete house;
	return 0 ;
}

4, Advantages and disadvantages

The main advantages are as follows:
Each specific builder is independent of each other, which is conducive to the expansion of the system.
The client does not need to know the details of the internal composition of the product, so it is easy to control the risk of details.

The disadvantages are as follows:
The components of the product must be the same, which limits its use.
If the internal changes of the product are complex, the pattern will add many builder classes.

5, The difference between builder mode and factory mode

Factory mode:
1. There is an abstract factory.
2. Realize a specific factory - automobile factory.
3. Factory A produces car A and gets car product A.
4. Factory B produces car B and gets car product B.
In this way, the buyer and the production line are isolated. The emphasis is on results.

Builder mode:
1. The engine factory produces the engine products and gets the car part A.
2. The tire factory produces wheel products and gets Auto Part B.
3. The chassis factory produces the body products and obtains the automobile parts C.
4. Put these parts together to form a whole that just can be assembled into a car.
5. Send the whole to the automobile assembly plant to get a automobile product.
The purpose of this method is to decouple the complex object production line and its components. The emphasis is on the process

The difference between the two is:
Factory mode does not consider the assembly process of objects, but directly generates an object I want.
The Builder pattern creates each part of an object one by one, and then assembles it into an object.
Factory mode solves the problem that the factory produces products.
The problem that the Builder pattern solves is that the factory controls the assembly process of each part of the product generator, and then obtains the product from the product generator.

6, Application scenario

The Builder pattern creates complex objects, and each part of its product is often faced with dramatic changes, but the algorithm that combines them is relatively stable, so it is usually used in the following occasions.
The objects created are complex and consist of many parts. Each part is faced with complex changes, but the order of construction is stable. The algorithm for creating complex objects is independent of the components of the objects and their assembly methods, that is, the product construction process and final representation are independent.

Posted by H4mm3r3r on Fri, 12 Jun 2020 23:59:53 -0700