“把大象装冰箱,总共分几步?”
“三步。第一步,打开冰箱门;第二步,把大象装进冰箱;第三步,把冰箱门关上。”
Jungle
活了这20多年,全靠这个笑话活着! 把大象装冰箱竟然只需要三步?那到底是怎么把大象装进冰箱呢?你问我,我问谁?再说,我也不关心这个呀!这……来点实际的吧,如果Jungle
要建一栋房子,总共分几步?本文的建造者模式将声情并茂地向您娓娓道来……
建造者模式将客户端与包含多个部件的复杂对象的创建过程分离,客户端不必知道复杂对象的内部组成方式与装配方式(就好像Jungle
不知道到底是如何把大象装进冰箱一样),只需知道所需建造者的类型即可。
建造者模式定义:
建造者模式:
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
“同样的构建过程可以创建不同的表示”??这句话是什么意思呢?想象一下,建造一栋房子,建造过程无非都是打地基、筑墙、安装门窗等过程,但不同的客户可能希望不同的风格或者过程,最终建造出来的房子当然就呈现不同的风格啦!
建造者模式的结构包含以下几个角色:
建造者模式UML类图如下:
考虑这样一个场景,如下图:
Jungle想要建造一栋简易的房子(地板、墙和天花板),两个工程师带着各自的方案找上门来,直接给Jungle看方案和效果图。
犹豫再三,Jungle最终选定了一位工程师……交房之日,Jungle满意的看着建好的房子,
开始思考:这房子究竟是怎么建成的呢?这地板、墙和天花板是怎么建造的呢?
工程师笑着说:“It's none of your business”
UML
图如下:
//产品类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(){
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;
};
House
是本实例中的产品,具有floor、wall和roof
三个属性。
//抽象建造者AbstractBall
class AbstractBuilder
{
public:
AbstractBuilder(){
house = new House();
}
virtual ~AbstractBuilder(){}
//抽象方法:
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");
}
//具体实现方法
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");
}
//具体实现方法
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(){}
//具体实现方法
void setBuilder(AbstractBuilder *iBuilder){
this->builder = iBuilder;
}
//封装组装流程,返回建造结果
House *construct(){
builder->buildFloor();
builder->buildWall();
builder->buildRoof();
return builder->getHouse();
}
private:
AbstractBuilder *builder;
};
#include "BuilderPattern.h"
int main()
{
//抽象建造者
AbstractBuilder *builder;
//指挥者
Director *director = new Director();
//产品:House
House *house;
//指定具体建造者A
builder = new ConcreteBuilderA();
director->setBuilder(builder);
house = director->construct();
house->printfHouseInfo();
delete builder;
builder = nullptr;
delete house;
house = nullptr;
//指定具体建造者B
builder = new ConcreteBuilderB();
director->setBuilder(builder);
house = director->construct();
house->printfHouseInfo();
delete builder;
builder = nullptr;
delete house;
house = nullptr;
delete director;
director = nullptr;
system("pause");
return 0;
}
BuilderPattern.h
#ifndef __BUILDER_PATTERN__
#define __BUILDER_PATTERN__
#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() {
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;
};
// 抽象建造者AbstractBall
class AbstractBuilder
{
public:
AbstractBuilder() {
house = new House();
}
AbstractBuilder(const AbstractBuilder& o) = delete;
AbstractBuilder& operator=(const AbstractBuilder& o) = delete;
virtual ~AbstractBuilder()
{
if (house != nullptr)
{
delete house;
house = nullptr;
}
}
// 抽象方法:
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(const ConcreteBuilderA& o) = delete;
ConcreteBuilderA& operator=(const ConcreteBuilderA& o) = delete;
~ConcreteBuilderA()
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
// 具体实现方法
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(const ConcreteBuilderB&) = delete;
ConcreteBuilderB& operator=(const ConcreteBuilderB&) = delete;
~ConcreteBuilderB()
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
// 具体实现方法
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)
{
delete builder;
builder = nullptr;
}
}
Director(const Director&) = delete;
Director& operator=(const Director&) = delete;
// 具体实现方法
void setBuilder(AbstractBuilder *iBuilder) {
this->builder = iBuilder;
}
// 封装组装流程,返回建造结果
House *construct() {
builder->buildFloor();
builder->buildWall();
builder->buildRoof();
return builder->getHouse();
}
private:
AbstractBuilder *builder;
};
#endif //__BUILDER_PATTERN__
main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "BuilderPattern.h"
int main()
{
// 抽象建造者
AbstractBuilder *builder;
// 指挥者
Director *director = new Director();
// 产品:House
House *house;
// 指定具体建造者A
builder = new ConcreteBuilderA();
director->setBuilder(builder);
house = director->construct();
house->printfHouseInfo();
delete builder;
// 指定具体建造者A
builder = new ConcreteBuilderB();
director->setBuilder(builder);
house = director->construct();
house->printfHouseInfo();
system("pause");
delete director;
delete builder;
return 0;
}
ConcreteBuilderA
Floor:Floor_A
Wall:Wall_A
Roof:Roof_A
ConcreteBuilderB
Floor:Floor_B
Wall:Wall_B
Roof:Roof_B
从客户端代码可以看到,客户端只需指定具体建造者,并作为参数传递给指挥者,通过指挥者即可得到结果。客户端无需关心House
的建造方法和具体流程。如果要更换建造风格,只需更换具体建造者即可,不同建造者之间并无任何关联,方便替换。从代码优化角度来看,其实可以不需要指挥者Director
的角色,而直接把construct
方法放入具体建造者当中。
以上就是今天要讲的内容,本文详细介绍了设计模式中创建型模式之建造者模式(BuilderPattern)
介绍,建造者模式(BuilderPattern)
设计模式的使用,设计模式提供了大量的方法供我们使用,非常的便捷,我们务必掌握。希望大家多多支持!另外如果上述有任何问题,请懂哥指教,不过没关系,主要是自己能坚持,更希望有一起学习的同学可以帮我指正,但是如果可以请温柔一点跟我讲,爱与和平是永远的主题,爱各位了。加油啊!
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_44918090/article/details/125862499
内容来源于网络,如有侵权,请联系作者删除!