(4 messages) How many steps are there to build a house? Builder mode tells you! _ Von Jungle’s personal blog -CSDN blog
Builder pattern structure
The structure of the Builder pattern contains the following roles:
- AbstractBuilder: Creates an abstract interface specified by the parts of a Product object;
- ConcreteBuilder: An interface that implements AbstractBuilder, implements concrete construction and assembly methods for individual components, and returns the creation result.
- Product: A concrete Product object
- Director: Builds an object that uses the Builder interface to arrange the construction of complex objects. The client typically only needs to interact with the Director, specify the Builder type, and pass the specific Builder object to the Director through a constructor or setter method. Its main role is: isolation of customer and object production process, and responsible for the control of product object production process.
The Builder pattern UML class diagram looks like this:
3. Builder pattern code instance
Consider the following scenario:
Jungle wanted to build a simple house (floor, wall and ceiling) and two engineers came with their own plans and showed Jungle the plans and renderings directly. After much hesitation, Jungle finally settled on an engineer… On the day of handing over the house, Jungle looked at the finished house with satisfaction and began to think: how on earth was this house built? How were the floors, walls and ceilings built? The engineer smiled and said, “It’s None of your Business.”
The UML diagram is as follows:
// Product category 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;
}
// Print House information
void printfHouseInfo(a){
printf("Floor:%s\t\n".this->floor.c_str());
printf("Wall:%s\t\n".this->wall.c_str());
printf("Roof:%s\t\n".this->roof.c_str());
}
private:
string floor;
string wall;
string roof;
};
Copy the code
Abstract builder AbstractBall
class AbstractBuilder
{
public:
AbstractBuilder(){
house = new House(a); }// Abstract method:
virtual void buildFloor(a) = 0;
virtual void buildWall(a) = 0;
virtual void buildRoof(a) = 0;
virtual House *getHouse(a) = 0;
House *house;
};
Copy the code
// ConcreteBuilderA
class ConcreteBuilderA :public AbstractBuilder
{
public:
ConcreteBuilderA() {printf("ConcreteBuilderA\n");
}
// How to implement it
void buildFloor(a){
this->house->setFloor("Floor_A");
}
void buildWall(a){
this->house->setWall("Wall_A");
}
void buildRoof(a){
this->house->setRoof("Roof_A");
}
House *getHouse(a){
return this->house; }};// ConcreteBuilderB
class ConcreteBuilderB :public AbstractBuilder
{
public:
ConcreteBuilderB() {printf("ConcreteBuilderB\n");
}
// How to implement it
void buildFloor(a){
this->house->setFloor("Floor_B");
}
void buildWall(a){
this->house->setWall("Wall_B");
}
void buildRoof(a){
this->house->setRoof("Roof_B");
}
House *getHouse(a){
return this->house; }};Copy the code
// The Director
class Director
{
public:
Director() {}// How to implement it
void setBuilder(AbstractBuilder *iBuilder){
this->builder = iBuilder;
}
// Encapsulate the assembly process and return the build result
House *construct(a){
builder->buildFloor(a); builder->buildWall(a); builder->buildRoof(a);return builder->getHouse(a); }private:
AbstractBuilder *builder;
};
Copy the code
Client code examples
#include "BuilderPattern.h"
int main(a)
{
// Abstract builder
AbstractBuilder *builder;
/ / commanders
Director *director = new Director(a);// Product: House
House *house;
// Specify builder A
builder = new ConcreteBuilderA(a); director->setBuilder(builder);
house = director->construct(a); house->printfHouseInfo(a);// Specify builder B
builder = new ConcreteBuilderB(a); director->setBuilder(builder);
house = director->construct(a); house->printfHouseInfo(a);system("pause");
return 0;
}
Copy the code
- The effect