![](https://i-blog.csdnimg.cn/blog_migrate/4818ce57c51888696b4519fbebf33c7a.png)
//工厂模式将对象的产生由工厂类进行逻辑控制
//建造者模式,多了一个设计者类,用于控制生产品的流程,适用于产品类较复杂,
//目的在于复用设计者类(业务流程控制类)
#include<iostream>
#include<string>
using namespace std;
class house
{
public:
void print_house()
{
cout << door << endl;
cout << window << endl;
cout << wall << endl;
}
public:
string door, window, wall;
};
//虚基类,用于生成产品,定义功能
class builder
{
public:
builder()
{
my_house = new house;
}
virtual void set_door() = 0;
virtual void set_window() = 0;
virtual void set_wall() = 0;
house *get_house()
{
return my_house;
}
protected:
house *my_house;
};
//依赖于虚基类,解耦合,定义业务流程
class derector
{
public:
house* consract(builder* work)
{
work->set_door(); //多态发生
work->set_wall();
work->set_window();
return work->get_house();
}
};
//定义功能细节
class valla_builder :public builder
{
public:
virtual void set_door()
{
my_house->door = "valla door";
}
virtual void set_window()
{
my_house->window = "valla window";
}
virtual void set_wall()
{
my_house->wall = "valla wall";
}
};
class flat_builder :public builder
{
public:
virtual void set_door()
{
my_house->door = "flat door";
}
virtual void set_window()
{
my_house->window = "flat window";
}
virtual void set_wall()
{
my_house->wall = "flat wall";
}
};
void main()
{
builder* work = new flat_builder;
derector design;
house *myhouse = design.consract(work);//简化业务逻辑控制流程
myhouse->print_house();
delete work;
delete myhouse;
return;
}