2016.4补充:

建造者模式实际上在c++中就是多态的表现,建立一个纯虚基类,不同的对象继承同一个纯虚基类,这样就可以在各自的类中去实现自己的方法而已,很简单的一个模式而已,这种模式在c++平常的不能再平常了,不要被总结成一种模式而不认识了或吓坏了,模式都是一种外在表现而已,不要被模式而模式. 关键是理解其中的思想。


建造者模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

举个经典的例子:

建造小人。建造一个小人,要分为六步:头部、身体、左手、右手、左脚和右脚。与抽象工厂模式不同的是,建造者模式是在Director的控制下一步一步的构造出来的,在建造的过程中,建造者模式可以进行更精细的控制。不管人的头部、身体、左手、右手、左脚或者右脚如何变化,但是最终还是由这几部分组合在一起形成一个人,虽然是同一个建造过程,但是这个人就会有不同的表示,比如,胖子,瘦子,个高的,个低的等等。


意图:

将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

适用性:

当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。

当构造过程必须允许被构造的对象有不同的表示时。

#include<iostream>

using namespace std;

class Builder
{
public:
virtual void BuildHead(){}
virtual void BuildBody(){}
virtual void BuildLeftHand(){}
virtual void BuildRightHand(){}
virtual void BuildLeftFoot(){}
virtual void BuildRightFoot(){}
};

//构造瘦子
class ThinBuilder : public Builder
{
public:
void BuildHead() { cout << "build thin body" << endl; }
void BuildBody() { cout << "build thin head" << endl; }
void BuildLeftHand() { cout << "build thin lefhand" << endl; }
void BuildRightHand() { cout << "build thin righhand" << endl; }
void BuildLeftFoot() { cout << "build thin leftfoot" << endl; }
void BuildRightFoot() { cout << "build thin rightfoot" << endl; }
};

//构造胖子
class FatBuilder : public Builder
{
public:
void BuildHead() { cout << "build fat body" << endl; }
void BuildBody() { cout << "build fat head" << endl; }
void BuildLeftHand() { cout << "build fat lefhand" << endl; }
void BuildRightHand() { cout << "build fat righhand" << endl; }
void BuildLeftFoot() { cout << "build fat leftfoot" << endl; }
void BuildRightFoot() { cout << "build fat rightfoot" << endl; }
};

//构造的指挥官
void creatman(Builder *type)
{
type->BuildBody();
type->BuildHead();
type->BuildLeftFoot();
type->BuildLeftHand();
type->BuildRightFoot();
type->BuildRightHand();
}


int main()
{
FatBuilder fat;
creatman(&fat);
}



对面上面代码进一步改 : 把creatman也封装一个类(指挥官的类)

#include<iostream>

using namespace std;

class Builder
{
public:
virtual void BuildHead(){}
virtual void BuildBody(){}
virtual void BuildLeftHand(){}
virtual void BuildRightHand(){}
virtual void BuildLeftFoot(){}
virtual void BuildRightFoot(){}
};
//构造瘦子
class ThinBuilder : public Builder
{
public:
void BuildHead() { cout << "build thin body" << endl; }
void BuildBody() { cout << "build thin head" << endl; }
void BuildLeftHand() { cout << "build thin lefhand" << endl; }
void BuildRightHand() { cout << "build thin righthand" << endl; }
void BuildLeftFoot() { cout << "build thin leftfoot" << endl; }
void BuildRightFoot() { cout << "build thin rightfoot" << endl; }
};

//构造胖子
class FatBuilder : public Builder
{
public:
void BuildHead() { cout << "build fat body" << endl; }
void BuildBody() { cout << "build fat head" << endl; }
void BuildLeftHand() { cout << "build fat lefhand" << endl; }
void BuildRightHand() { cout << "build fat righthand" << endl; }
void BuildLeftFoot() { cout << "build fat leftfoot" << endl; }
void BuildRightFoot() { cout << "build fat rightfoot" << endl; }
};

//构造的指挥官
class Director
{
public:
Director(Builder *builder) { m_Builder = builder; }
void CreateMan()
{
m_Builder->BuildHead();
m_Builder->BuildBody();
m_Builder->BuildLeftHand();
m_Builder->BuildRightHand();
m_Builder->BuildLeftFoot();
m_Builder->BuildRightFoot();
}

private:
Builder *m_Builder;
};

int main()
{
FatBuilder thin;
Director director(&thin);
director.CreateMan();
return 0;
}


对面上面代码进一步改 : 再改复杂一点,把造的人也封装起来

#include<iostream>

using namespace std;

typedef enum
{
FatMan,
ThinMan,
NormalMan,
}e_MnaType;


class Man
{
public:
Man() {}
Man(e_MnaType s) :m_Type(s){}

void SetHead(e_MnaType type){ }
void SetBody(e_MnaType type){ }
void SetLeftHand(e_MnaType type){ }
void SetRightHand(e_MnaType type){ }
void SetLeftFoot(e_MnaType type){ }
void SetRightFoot(e_MnaType type){ }

void ShowMan()
{
switch (m_Type)
{
case FatMan:
cout << "I'm a ffffffffat man" << endl;
break;

case ThinMan:
cout << "I'm a thin man" << endl;
break;

default:
cout << "I'm a normal man" << endl;
break;
}
}

private:
e_MnaType m_Type;
};

class Builder
{
public:
virtual void BuildHead(){}
virtual void BuildBody(){}
virtual void BuildLeftHand(){}
virtual void BuildRightHand(){}
virtual void BuildLeftFoot(){}
virtual void BuildRightFoot(){};
virtual Man GetMan() = 0;
};

// FatManBuilder
class FatManBuilder : public Builder
{
public:
FatManBuilder() { m_FatMan = FatMan; }
void BuildHead(){ m_FatMan.SetHead(FatMan); }
void BuildBody(){ m_FatMan.SetBody(FatMan); }
void BuildLeftHand(){ m_FatMan.SetLeftHand(FatMan); }
void BuildRightHand(){ m_FatMan.SetRightHand(FatMan); }
void BuildLeftFoot(){ m_FatMan.SetLeftFoot(FatMan); }
void BuildRightFoot(){ m_FatMan.SetRightFoot(FatMan); }

Man GetMan() { return m_FatMan; }

private:
Man m_FatMan;
};

// ThisManBuilder
class ThinManBuilder : public Builder
{
public:
ThinManBuilder(){ m_ThinMan = ThinMan; }
void BuildHead(){ m_ThinMan.SetHead(ThinMan); }
void BuildBody(){ m_ThinMan.SetBody(ThinMan); }
void BuildLeftHand(){ m_ThinMan.SetLeftHand(ThinMan); }
void BuildRightHand(){ m_ThinMan.SetRightHand(ThinMan); }
void BuildLeftFoot(){ m_ThinMan.SetLeftFoot(ThinMan); }
void BuildRightFoot(){ m_ThinMan.SetRightFoot(ThinMan); }

Man GetMan() { return m_ThinMan; }

private:
Man m_ThinMan;
};


// Director
class Director
{
public:
Director(Builder *builder) { m_Builder = builder; }
void CreateMan()
{
m_Builder->BuildHead();
m_Builder->BuildBody();
m_Builder->BuildLeftHand();
m_Builder->BuildRightHand();
m_Builder->BuildLeftHand();
m_Builder->BuildRightHand();
}

private:
Builder *m_Builder;
};

int main(int argc, char *argv[])
{
Builder *builderObj = new ThinManBuilder();
Director directorObj(builderObj);
directorObj.CreateMan();

Man manObj = builderObj->GetMan();;
manObj.ShowMan();
return 0;
};





UML图23个设计模式学习完了再统一复习总结的时候画出来