在GOF的《設計模式 可復用面向對象軟件的基礎》中是這樣說的:將一個復雜對象的構建與它的表示分離,使得同樣的構建過程可以創建不同的表示。
這句話,似懂非懂的。一個復雜對象的創建,其通常是由很多的子對象構成;如果一個對象能夠直接就創建好了,那麼也不會稱之為復雜對象。由於項目中需求的變化,這個復雜對象的各個部分經常會發生劇烈的變化,但是,不管怎麼變化,將它們組合在一起,組成一個復雜的對象的事實是不會變的。建造者模式就提供了一種“封裝機制”來將各個對象的變化隔離開,最終,組合成復雜對象的過程是不會變的。
在《大話設計模式》一書中,例舉了一個很好的例子————建造小人。建造一個小人,要分為六步:頭部、身體、左手、右手、左腳和右腳。與抽象工廠模式不同的是,建造者模式是在Director的控制下一步一步的構造出來的,在建造的過程中,建造者模式可以進行更精細的控制。不管人的頭部、身體、左手、右手、左腳或者右腳如何變化,但是最終還是由這幾部分組合在一起形成一個人,雖然是同一個建造過程,但是這個人就會有不同的表示,比如,胖子,瘦子,個高的,個低的等等。
類圖如下:
時序圖如下:
/*
** FileName : BuilderPattern
** Author : Jelly Young
** Date : 2013/11/22
** Description : More information, please go to http://www.jellythink.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;};// Builderclass 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; }};// FatManBuilderclass 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;};// ThisManBuilderclass 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;};// Directorclass 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 manObj; // 感謝張小張同學的review
manObj = NULL;
delete builderObj;
builderObj = NULL;
return 0;};