设计模式学习(三) 建造者模式 Builder

1. 建造者模式的基本概念与构成

建造者模式,通俗点来说,就是设计者,工程队与产品之间的三方联调模式,具体产品中包含自己的成员,以及对成员的定义与读取方式;而工程队分为抽象构造者和具体构造者,抽象构造者中均为虚函数,用于提供所有具体构造者的接口,而具体构造者也就是直接通过接口,对应具体产品中的变量与函数来实现具体的功能。设计者则负责直接调用抽象工程队,这样就在无需更改代码的情况下可以随意使用继承自抽象工程队的所有具体工程队了。
其具体的类图可以表述如下:
建造者模式类图
通过类图中可知,抽象工程队与设计者之间为标准的聚合关系,即两者的联系相对于普通的关联关系更为密切,同时也代表着设计者对应的抽象工程队可以不止一个,对应不同的抽象基类,重载不同的构造函数即可。

2. 建造者模式的代码实现以及个人理解

#include <iostream>
#include <string>
using namespace std;

class House		//产品类,根据实际情况对其进行更改
{
public:
	void setDoor(string door)
	{
		m_door = door;
	}
	void setWindow(string window)
	{
		m_window = window;
	}
	void setWall(string wall)
	{
		m_wall = wall;
	}
	string getDoor()
	{
		return m_door;
	}
	string getWindow()
	{
		return m_window;
	}
	string getWall()
	{
		return m_wall;
	}
private:
	string m_door;
	string m_window;
	string m_wall;
};

class Build    //build的抽象基类
{
public:
	virtual void makeDoor() = 0;
	virtual void makeWindow() = 0;
	virtual void makeWall() = 0;
	virtual House* getHouse() = 0;
};

class normalBuild : public Build
{
public:
	normalBuild()
	{
		house = new House;
	}
	void makeDoor() 
	{ 
		house->setDoor("normaldoor");
		cout << "make normal door!" << endl; 
	}
	void makeWindow()
	{
		house->setWindow("normalwindow");
		cout << "make normal window!" << endl;
	}
	void makeWall()
	{
		house->setWall("normalwall");
		cout << "make normal wall!" << endl;
	}
	House* getHouse()
	{
		return house;
	}
private:
	House *house;
};

class GreatBuild : public Build
{
public:
	GreatBuild()
	{
		house = new House;
	}
	void makeDoor()
	{
		house->setDoor("Greatdoor");
		cout << "make Great door!" << endl;
	}
	void makeWindow()
	{
		house->setWindow("Greatwindow");
		cout << "make Great window!" << endl;
	}
	void makeWall()
	{
		house->setWall("Greatwall");
		cout << "make Great wall!" << endl;
	}
	House* getHouse()
	{
		return house;
	}
private:
	House *house;
};

class Director  //指挥者,直接调用抽象基类来执行,根据其特性会自动匹配
{
public:
	Director(Build* build)
	{
		m_build = build;
	}
	void construct()
	{
		m_build->makeWall();
		m_build->makeDoor();
		m_build->makeWindow();
	}
private:
	Build *m_build;
};

int main()
{
	Director *director = nullptr;

	Build *first_build = new normalBuild;
	director = new Director(first_build);
	director->construct();
	House *first_house = new House;
	first_house = first_build->getHouse();
	cout << first_house->getDoor() << endl << first_house->getWindow() << endl << first_house->getWall() << endl ;
	delete first_build;
	delete first_house;

	Build *second_build = new GreatBuild;
	director = new Director(second_build);
	director->construct();
	House *second_house = new House;
	second_house = second_build->getHouse();
	cout << second_house->getDoor() << endl << second_house->getWindow() << endl << second_house->getWall() << endl;
	delete second_build;
	delete second_house;

	return 0;
}

我个人认为建造者模式是在代码编写过程中最容易使用到的模式,因为实际开发过程中的不同功能的实现过程往往会相对复杂一些,所以需要建造者模式将对象的构建与表示给分离出来。其中构建过程就是指挥者指挥工程队构造的这段过程,也就是类图的左边三块;而表示的部分自然是完成构建后,对于产品自身的操作。这就可以类比于Qt设计中的底层—中间—UI的方式,在底层完成对于对象的构建过程,然后通过中间层,将底层对象的数据与UI连接起来,最终由UI显示结果。这样的好处在于即使没有了UI,也能够随意的调用底层的对象,这就是建造者模式的本意:设计者调用构造者建立了对象,但是并不是非得使用这个对象。当失去了具体产品的对象后,构造的对象仍然可以独立存在,其是否被调用那又是另一回事了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方寸间沧海桑田

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值