建造者模式
在GOF的《设计模式 可复用面向对象软件的基础》中是这样说的:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
这句话,似懂非懂的。一个复杂对象的创建,其通常是由很多的子对象构成;如果一个对象能够直接就创建好了,那么也不会称之为复杂对象。由于项目中需求的变化,这个复杂对象的各个部分经常会发生剧烈的变化,但是,不管怎么变化,将它们组合在一起,组成一个复杂的对象的事实是不会变的。建造者模式就提供了一种“封装机制”来将各个对象的变化隔离开,最终,组合成复杂对象的过程是不会变的。
在《大话设计模式》一书中,例举了一个很好的例子————建造小人。建造一个小人,要分为六步:头部、身体、左手、右手、左脚和右脚。与抽象工厂模式不同的是,建造者模式是在Director的控制下一步一步的构造出来的,在建造的过程中,建造者模式可以进行更精细的控制。不管人的头部、身体、左手、右手、左脚或者右脚如何变化,但是最终还是由这几部分组合在一起形成一个人,虽然是同一个建造过程,但是这个人就会有不同的表示,比如,胖子,瘦子,个高的,个低的等等。
UML图
类图如下:
时序图如下:
代码实现
/*
** FileName : BuilderPattern
** Author : Jelly Young
** Date : 2013/11/22
** Description : More information, please go to http://www.weikejianghu.com
*/
#include <iostream>
using namespace std;
typedef enum MANTYPETag
{
kFatMan,
kThinMan,
kNormal
}MANTYPE;
class Man
{
public:
void SetHead(MANTYPE type){ m_Type = type; }
void SetBody(MANTYPE type){ m_Type = type; }
void SetLeftHand(MANTYPE type){ m_Type = type; }
void SetRightHand(MANTYPE type){ m_Type = type; }
void SetLeftFoot(MANTYPE type){ m_Type = type; }
void SetRightFoot(MANTYPE type){ m_Type = type; }
void ShowMan()
{
switch (m_Type)
{
case kFatMan:
cout<<"I'm a fat man"<<endl;
return;
case kThinMan:
cout<<"I'm a thin man"<<endl;
return;
default:
cout<<"I'm a normal man"<<endl;
return;
}
}
private:
MANTYPE m_Type;
};
// Builder
class Builder
{
public:
virtual void BuildHead(){}
virtual void BuildBody(){}
virtual void BuildLeftHand(){}
virtual void BuildRightHand(){}
virtual void BuildLeftFoot(){}
virtual void BuildRightFoot(){}
virtual Man *GetMan(){ return NULL; }
};
// FatManBuilder
class FatManBuilder : public Builder
{
public:
FatManBuilder(){ m_FatMan = new Man(); }
void BuildHead(){ m_FatMan->SetHead(kFatMan); }
void BuildBody(){ m_FatMan->SetBody(kFatMan); }
void BuildLeftHand(){ m_FatMan->SetLeftHand(kFatMan); }
void BuildRightHand(){ m_FatMan->SetRightHand(kFatMan); }
void BuildLeftFoot(){ m_FatMan->SetLeftFoot(kFatMan); }
void BuildRightFoot(){ m_FatMan->SetRightFoot(kFatMan); }
Man *GetMan(){ return m_FatMan; }
private:
Man *m_FatMan;
};
// ThisManBuilder
class ThinManBuilder : public Builder
{
public:
ThinManBuilder(){ m_ThinMan = new Man(); }
void BuildHead(){ m_ThinMan->SetHead(kThinMan); }
void BuildBody(){ m_ThinMan->SetBody(kThinMan); }
void BuildLeftHand(){ m_ThinMan->SetLeftHand(kThinMan); }
void BuildRightHand(){ m_ThinMan->SetRightHand(kThinMan); }
void BuildLeftFoot(){ m_ThinMan->SetLeftFoot(kThinMan); }
void BuildRightFoot(){ m_ThinMan->SetRightFoot(kThinMan); }
Man *GetMan(){ return m_ThinMan; }
private:
Man *m_ThinMan;
};
// Director
class Director
{
public:
Director(Builder *builder) { m_Builder = builder; }
void CreateMan();
private:
Builder *m_Builder;
};
void Director::CreateMan()
{
m_Builder->BuildHead();
m_Builder->BuildBody();
m_Builder->BuildLeftHand();
m_Builder->BuildRightHand();
m_Builder->BuildLeftHand();
m_Builder->BuildRightHand();
}
int main(int argc, char *argv[])
{
Builder *builderObj = new FatManBuilder();
Director directorObj(builderObj);
directorObj.CreateMan();
Man *manObj = builderObj->GetMan();
if (manObj == NULL)
return 0;
manObj->ShowMan();
delete builderObj;
builderObj = NULL;
return 0;
};
</div>
上面这个例子比较杂,但是也是建造者模式的应用。下面这个例子是建造者最一般,最简单的实现方法:
/*
** FileName : BuilderPattern
** Author : Jelly Young
** Date : 2013/11/23
** Description : More information, please go to http://www.weikejianghu.com
*/
#include <iostream>
#include <vector>
using namespace std;
class Builder;
// Product
class Product<