#include <stdio.h>
#include <iostream>
using namespace std;
class PersonBuilder {
protected:
string g;
string p;
public:
PersonBuilder (string graphy,string pen)
{
g = graphy;
p = pen;
};
virtual ~PersonBuilder(){};
virtual void BuildHead(){};
virtual void BuildBody(){};
};
class PersonThinBuilder:public PersonBuilder {
public:
PersonThinBuilder(string graphy,string pen):PersonBuilder(graphy,pen){};
void BuildHead(){
cout<<g<< "draw thin head" <<p<< endl;
};
void BuildBody(){
cout<<g<< "draw thin body" <<p<< endl;
};
};
class PersonFatBuilder:public PersonBuilder {
public:
PersonFatBuilder(string graphy,string pen):PersonBuilder(graphy,pen){};
void BuildHead(){
cout<<g<< "draw fat head" <<p<< endl;
};
void BuildBody(){
cout<<g<< "draw fat body" <<p<< endl;
};
};
class PersonDirector {
private:
PersonBuilder * pb;
public:
PersonDirector(PersonBuilder * pb) {
this->pb = pb;
}
void CreatePerson(){
(*pb).BuildHead();
(*pb).BuildBody();
}
};
string pen = "yellow";
string graphic = "green";
PersonThinBuilder * ptb = new PersonThinBuilder(graphic,pen);
PersonDirector * pdThin = new PersonDirector(ptb);
pdThin->CreatePerson();
PersonFatBuilder * pfb = new PersonFatBuilder(graphic,pen);
PersonDirector * pdFat = new PersonDirector(pfb);
pdFat->CreatePerson();
建造者模式重要概念
<h1>很不好理解
基本概念: 将一个复杂概念的构造与他的表示分离,使得同样的构造过程可以创建不同的表示。如果我们指定了建造者模式,那么用户只要指定需要建造的类型就可以得到他们。(我觉得这个例子当中使用范型会更加好,任何可以使用某个活着某些方法的东西都可以个给这个builder,甚至给的是个类型都可以)