设计模式最大的作用就是在变化和稳定中间寻找隔离点,然后分离它们,从而管理变化。将变化像小兔子一样关到笼子里,让它在笼子里随便跳,而不至于跳出来把你整个房间给污染掉。
设计思想
将一个复杂对象的构建与其表示相分离,使得同样的构建过程(稳定)可以创建不同的表示(变化)
动机
在软件系统中,有时候面临着“一个复杂对象” 的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定
如何应对这种变化,如何提供一种“封装机制”来隔离出“复杂对象的各个部分”的变化,从而保证系统中的“稳定构建算法”不随着需求改变而改变?
结构
要点总结
Builder 模式主要用于“分步骤构建一个复杂的对象”。在这其中“ 分步骤 ”是一个稳定的算法,而复杂对象的各个部分则经常变化
变化点在哪里,封装就在哪里 — Builder 模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动
在Builder 模式中,要注意不同语言中构造器内调用抽象方法的差别。
业务场景
假如你要建房子,建房子需要打地基,需要买建材垒墙,需要造窗户等等,构造一个“房子”对象似乎并不是一件简单的事情…而现在,需要你用不同建材的材料分别去建房子,这些过程一样,但是材料不一样,该如何设计?
代码案例
#include <iostream>
#include <memory>
#include <vector>
using namespace std;class House
{
public:House() {}~House(){cout << "拆房..." << endl;}void init(){build1();build2();if (build3()){for (int i = 0; i < 4; i++){build4(i);}}build5();}protected:virtual void build1() const = 0;virtual void build2() const = 0;virtual bool build3() const = 0;virtual void build4(const int i) const = 0;virtual void build5() const = 0;
};class storeHouse : public House
{
private:virtual void build1() const override{cout << "用石头打地基..." << endl;}virtual void build2() const override{cout << "用石头造房子..." << endl;}virtual bool build3() const override{return cout << "通过质检..." << endl, true;}virtual void build4(const int i) const override{cout << "造第" << (i + 1) << "扇窗户" << endl;}virtual void build5() const override{cout << "石头房子竣工..." << endl;}
};class glassHouse : public House
{
private:virtual void build1() const override{cout << "用玻璃打地基..." << endl;}virtual void build2() const override{cout << "用玻璃造房子..." << endl;}virtual bool build3() const override{return cout << "通过质检..." << endl, true;}virtual void build4(const int i) const override{cout << "造第" << (i + 1) << "扇窗户" << endl;}virtual void build5() const override{cout << "玻璃房子竣工..." << endl;}
};class treeHouse : public House
{
private:virtual void build1() const override{cout << "用木头打地基..." << endl;}virtual void build2() const override{cout << "用木头造房子..." << endl;}virtual bool build3() const override{return cout << "通过质检..." << endl, true;}virtual void build4(const int i) const override{cout << "造第" << (i + 1) << "扇窗户" << endl;}virtual void build5() const override{cout << "木头房子竣工..." << endl;}
};int main()
{std::vector<std::shared_ptr<House>> myHouse;myHouse.push_back(make_shared<storeHouse>());myHouse.push_back(make_shared<glassHouse>());myHouse.push_back(make_shared<treeHouse>());for (const auto& item : myHouse){item->init();cout << "-----------------------------" << endl;}return 0;
}
这里和之前的模板方法模式很像,但是这里把House中的部分属性忽略了,仅仅暴露出其建造过程,于是,如果采用构建器模式,可以单独把建造过程抽出来:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;class House
{
public:House() {}~House(){cout << "拆房..." << endl;}public:virtual void build1() const = 0;virtual void build2() const = 0;virtual bool build3() const = 0;virtual void build4(const int i) const = 0;virtual void build5() const = 0;
};class storeHouse : public House
{
public:virtual void build1() const override{cout << "用石头打地基..." << endl;}virtual void build2() const override{cout << "用石头造房子..." << endl;}virtual bool build3() const override{return cout << "通过质检..." << endl, true;}virtual void build4(const int i) const override{cout << "造第" << (i + 1) << "扇窗户" << endl;}virtual void build5() const override{cout << "石头房子竣工..." << endl;}
};class glassHouse : public House
{
public:virtual void build1() const override{cout << "用玻璃打地基..." << endl;}virtual void build2() const override{cout << "用玻璃造房子..." << endl;}virtual bool build3() const override{return cout << "通过质检..." << endl, true;}virtual void build4(const int i) const override{cout << "造第" << (i + 1) << "扇窗户" << endl;}virtual void build5() const override{cout << "玻璃房子竣工..." << endl;}
};class treeHouse : public House
{
public:virtual void build1() const override{cout << "用木头打地基..." << endl;}virtual void build2() const override{cout << "用木头造房子..." << endl;}virtual bool build3() const override{return cout << "通过质检..." << endl, true;}virtual void build4(const int i) const override{cout << "造第" << (i + 1) << "扇窗户" << endl;}virtual void build5() const override{cout << "木头房子竣工..." << endl;}
};class HouseBuilder
{private:std::shared_ptr<House> _house;public:HouseBuilder(const std::shared_ptr<House>& _house): _house(_house){}void init(){_house->build1();_house->build2();if (_house->build3()){for (int i = 0; i < 4; i++){_house->build4(i);}}_house->build5();}
};int main()
{std::vector<std::shared_ptr<HouseBuilder>> myHouse;myHouse.push_back(make_shared<HouseBuilder>(make_shared<storeHouse>()));myHouse.push_back(make_shared<HouseBuilder>(make_shared<glassHouse>()));myHouse.push_back(make_shared<HouseBuilder>(make_shared<treeHouse>()));for (const auto& item : myHouse){item->init();cout << "-----------------------------" << endl;}return 0;
}