個人感覺這個模式,使用的應該較少,主要應用於流程性的東西,廢話不多說,讓我們進入生成器模式。
意圖:
將一個復雜對象的構建與它的表示分離,使得同樣的構建過程可以創建不同的表示。
適用性:
1、當創建復雜對象的算法應該獨立於該對象的組成部分以及它們的裝配方式時
2、當構造過程必須允許被構造的對象有不同的表示時
使用生成器模式的基本方法:
1、需要抽象出一個基類,所有的對象創建時,都繼承自這個基類創建。
2、需要創建一個調用的導演,由它來多態到不同的表現。(它並不繼承自那個抽象基類,而是存儲一個抽象基類的指針,類似多態)。
3、各個具體對象實例化基類的接口
看一個生成器模式的基本結構例子,本例子從http://www.cppblog.com/converse/archive/2006/07/21/10305.html,整理,僅供學習使用:
builder.h
//設計模式之builder模式
/*
作用:將一個復雜對象的構建與它的表示分離,使同樣的構建過程創建不同的表示
*/
//虛擬基類,所有builder的基類,提供不同部分的構建接口
class builder
{
public:
builder(){};
virtual ~builder(){};
//提供構建接口
virtual void builderA() = 0;
virtual void builderB() = 0;
};
//使用builder構建產品,構建產品的過程都一致,但是不同的builder會有不同的表現
//這個不同的實現通過不同的builder派生類來實現,存有一個builder的指針,多態調用
class director
{//
public:
director(builder *pbuilder);
virtual ~director();
void construct();
private:
builder *pBuilder;
};
//具體實例化各個建造者
class concretebuilder1:public builder
{
public:
concretebuilder1(){}
virtual ~concretebuilder1(){}
virtual void builderA();
virtual void builderB();
};
//具體實例化各個建造者
class concretebuilder2:public builder
{
public:
concretebuilder2(){}
virtual ~concretebuilder2(){}
virtual void builderA();
virtual void builderB();
};
//設計模式之builder模式
/*
作用:將一個復雜對象的構建與它的表示分離,使同樣的構建過程創建不同的表示
*/
//虛擬基類,所有builder的基類,提供不同部分的構建接口
class builder
{
public:
builder(){};
virtual ~builder(){};
//提供構建接口
virtual void builderA() = 0;
virtual void builderB() = 0;
};
//使用builder構建產品,構建產品的過程都一致,但是不同的builder會有不同的表現
//這個不同的實現通過不同的builder派生類來實現,存有一個builder的指針,多態調用
class director
{//
public:
director(builder *pbuilder);
virtual ~director();
void construct();
private:
builder *pBuilder;
};
//具體實例化各個建造者
class concretebuilder1:public builder
{
public:
concretebuilder1(){}
virtual ~concretebuilder1(){}
virtual void builderA();
virtual void builderB();
};
//具體實例化各個建造者
class concretebuilder2:public builder
{
public:
concretebuilder2(){}
virtual ~concretebuilder2(){}
virtual void builderA();
virtual void builderB();
};
builder.cpp
#include "builder.h"
#include <stdio.h>
void concretebuilder1::builderA()
{
printf("builderA by concretebuilder1\n");
}
void concretebuilder1::builderB()
{
printf("builderB by concretebuilder1\n");
}
void concretebuilder2::builderA()
{
printf("builderA by concretebuilder2\n");
}
void concretebuilder2::builderB()
{
printf("builderB by concretebuilder2\n");
}
director::director(builder *pbuilder)
{
this->pBuilder = pbuilder;
}
director::~director()
{
delete pBuilder;
pBuilder = NULL;
}
void director::construct()
{
//將同一個過程的表示組裝一下
pBuilder->builderA();
pBuilder->builderB();
}
void main()
{
//調用時,需要注意一下,具體想創建哪個表示,就實例化哪個
builder *pbuilder1 = new concretebuilder1;
//通過導演,可以將表示提取出來
director *pdirector = new director(pbuilder1);
//具體操作,就可以由導演代勞了。
pdirector->construct();
builder *pbuilder2 = new concretebuilder2;
director *pdirector2 = new director(pbuilder2);
pdirector2->construct();
delete pdirector;
delete pdirector2;
}
#include "builder.h"
#include <stdio.h>
void concretebuilder1::builderA()
{
printf("builderA by concretebuilder1\n");
}
void concretebuilder1::builderB()
{
printf("builderB by concretebuilder1\n");
}
void concretebuilder2::builderA()
{
printf("builderA by concretebuilder2\n");
}
void concretebuilder2::builderB()
{
printf("builderB by concretebuilder2\n");
}
director::director(builder *pbuilder)
{
this->pBuilder = pbuilder;
}
director::~director()
{
delete pBuilder;
pBuilder = NULL;
}
void director::construct()
{
//將同一個過程的表示組裝一下
pBuilder->builderA();
pBuilder->builderB();
}
void main()
{
//調用時,需要注意一下,具體想創建哪個表示,就實例化哪個
builder *pbuilder1 = new concretebuilder1;
//通過導演,可以將表示提取出來
director *pdirector = new director(pbuilder1);
//具體操作,就可以由導演代勞了。
pdirector->construct();
builder *pbuilder2 = new concretebuilder2;
director *pdirector2 = new director(pbuilder2);
pdirector2->construct();
delete pdirector;
delete pdirector2;
}
分析代碼的時候主要看類的結構關系,以及具體的使用方法。
下面讓我們看一個具體的例子。
例子主要從http://www.cnblogs.com/wanggary/archive/2011/04/15/2017615.html,得來的,僅供學習使用。
carbuilder.h
/*
客戶模型:
carmodel:汽車模型
benmodel:奔馳汽車
bmwmodel:寶馬汽車
carbuilder:建造者接口
benbuilder:奔馳建造者
bmwbuilder:寶馬建造者
director:導演
*/
#include <vector>
#include <string>
#include <iostream>
using namespace std;
//首先虛擬出一個汽車模型
class carmodel
{
public:
carmodel();
virtual ~carmodel();
void Run();
void SetSequence(vector<string> *pSeq);
protected:
//定義出一系列操作
virtual void Start() = 0;
virtual void Stop() = 0;
virtual void Alarm() = 0;
virtual void EngineBoom() = 0;
private:
vector<string> *m_pSequence;
};
//根據汽車模型構建奔馳汽車
class benmodel:public carmodel
{
public:
benmodel();
~benmodel();
protected:
void Start();
void Stop();
void Alarm();
void EngineBoom();
};
//根據汽車模型構建寶馬汽車 www.2cto.com
class bmwmodel:public carmodel
{
public:
bmwmodel();
~bmwmodel();
protected:
void Start();
void Stop();
void Alarm();
void EngineBoom();
};
//虛擬出一個汽車的建造者模型
//類似於上面說的那個抽象基類
class carbuilder
{
public:
carbuilder()
{
}
virtual ~carbuilder()
{
}
virtual void SetSequence(vector<string> *pSeq) = 0;
virtual carmodel* GetCarModel() = 0;
};
//創建一個奔馳汽車
class benbuilder:public carbuilder
{
public:
benbuilder();
~benbuilder();
void SetSequence(vector<string> *pSeq);
carmodel* GetCarModel();
private:
carmodel *pBen;
};
//創建一個奔馳汽車
class bmwbuilder:public carbuilder
{
public:
bmwbuilder();
~bmwbuilder();
void SetSequence(vector<string> *pSeq);
carmodel* GetCarModel();
private:
carmodel *pBmw;
};
//這個就是導演,
class director
{
public:
director();
~director();
benmodel* GetABenModel();
benmodel* GetBBenModel();
bmwmodel* GetCBmwModel();
bmwmodel* GetDBmwModel();
private:
vector<string> *pSequence;
benbuilder *pBen;
bmwbuilder *pBmw;
};
/*
客戶模型:
carmodel:汽車模型
benmodel:奔馳汽車
bmwmodel:寶馬汽車
carbuilder:建造者接口
benbuilder:奔馳建造者
bmwbuilder:寶馬建造者
director:導演
*/
#include <vector>
#include <string>
#include <iostream>
using namespace std;
//首先虛擬出一個汽車模型
class carmodel
{
public:
carmodel();
virtual ~carmodel();
void Run();
void SetSequence(vector<string> *pSeq);
protected:
//定義出一系列操作
virtual void Start() = 0;
virtual void Stop() = 0;
virtual void Alarm() = 0;
virtual void EngineBoom() = 0;
private:
vector<string> *m_pSequence;
};
//根據汽車模型構建奔馳汽車
class benmodel:public carmodel
{
public:
benmodel();
~benmodel();
protected:
void Start();
void Stop();
void Alarm();
void EngineBoom();
};
//根據汽車模型構建寶馬汽車
class bmwmodel:public carmodel
{
public:
bmwmodel();
~bmwmodel();
protected:
void Start();
void Stop();
void Alarm();
void EngineBoom();
};
//虛擬出一個汽車的建造者模型
//類似於上面說的那個抽象基類
class carbuilder
{
public:
carbuilder()
{
}
virtual ~carbuilder()
{
}
virtual void SetSequence(vector<string> *pSeq) = 0;
virtual carmodel* GetCarModel() = 0;
};
//創建一個奔馳汽車
class benbuilder:public carbuilder
{
public:
benbuilder();
~benbuilder();
void SetSequence(vector<string> *pSeq);
carmodel* GetCarModel();
private:
carmodel *pBen;
};
//創建一個奔馳汽車
class bmwbuilder:public carbuilder
{
public:
bmwbuilder();
~bmwbuilder();
void SetSequence(vector<string> *pSeq);
carmodel* GetCarModel();
private:
carmodel *pBmw;
};
//這個就是導演,
class director
{
public:
director();
~director();
benmodel* GetABenModel();
benmodel* GetBBenModel();
bmwmodel* GetCBmwModel();
bmwmodel* GetDBmwModel();
private:
vector<string> *pSequence;
benbuilder *pBen;
bmwbuilder *pBmw;
};
carbuilder.cpp
#include"carbuilder.h"
carmodel::carmodel()
{
}
carmodel::~carmodel()
{
}
void carmodel::SetSequence(vector<string> *pSeq)
{
m_pSequence = pSeq;
}
void carmodel::Run()
{
vector<string>::const_iterator it = m_pSequence->begin();
for(;it != m_pSequence->end();++it )
{
string actionName = *it;
if(actionName.compare("start") == 0)
{
Start();
}
else if(actionName.compare("stop") == 0)
{
Stop();
}
else if(actionName.compare("alarm") == 0)
{
Alarm();
}
else if(actionName.compare("engine boom") == 0)
{
EngineBoom();
}
}
}
benmodel::benmodel()
{
}
benmodel::~benmodel()
{
}
void benmodel::Start()
{
cout<<"ben start..."<<endl;
}
void benmodel::Stop()
{
cout<<"ben stop..."<<endl;
}
void benmodel::Alarm()
{
cout<<"ben alarm..."<<endl;
}
void benmodel::EngineBoom()
{
cout<<"ben engineboom..."<<endl;
}
bmwmodel::bmwmodel()
{
}
bmwmodel::~bmwmodel()
{
}
void bmwmodel::Start()
{
cout<<"bmw start..."<<endl;
}
void bmwmodel::Stop()
{
cout<<"bmw stop..."<<endl;
}
void bmwmodel::Alarm()
{
cout<<"bmw alarm..."<<endl;
}
void bmwmodel::EngineBoom()
{
cout<<"bmw engineboom..."<<endl;
}
benbuilder::benbuilder()
{
//在構造函數中創建奔馳汽車的模型
pBen = new benmodel();
}
benbuilder::~benbuilder()
{
}
carmodel* benbuilder::GetCarModel()
{
return pBen;
}
void benbuilder::SetSequence(vector<string> *pSeq)
{
pBen->SetSequence(pSeq);
}
bmwbuilder::bmwbuilder()
{
//在構造函數中創建寶馬汽車的模型
pBmw = new bmwmodel();
}
bmwbuilder::~bmwbuilder()
{
}
carmodel* bmwbuilder::GetCarModel()
{
return pBmw;
}
void bmwbuilder::SetSequence(vector<string> *pSeq)
{
pBmw->SetSequence(pSeq);
}
director::director()
{
pBen = new benbuilder();
pBmw = new bmwbuilder();
pSequence = new vector<string>();
}
director::~director()
{
delete pBen;
delete pBmw;
delete pSequence;
}
benmodel * director::GetABenModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("stop");
pBen->SetSequence(pSequence);
return dynamic_cast<benmodel*>(pBen->GetCarModel());
}
benmodel * director::GetBBenModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("Alarm");
pSequence->push_back("Stop");
pBen->SetSequence(pSequence);
return dynamic_cast<benmodel*>(pBen->GetCarModel());
}
bmwmodel *director::GetCBmwModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("stop");
pBmw->SetSequence(pSequence);
return dynamic_cast<bmwmodel*>(pBmw->GetCarModel());
}
bmwmodel *director::GetDBmwModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("Engineboom");
pBmw->SetSequence(pSequence);
return dynamic_cast<bmwmodel*>(pBmw->GetCarModel());
}
int main()
{
director *pdir = new director;
benmodel *pben = pdir->GetABenModel();
pben->Run();
bmwmodel *pbmw = pdir->GetCBmwModel();
pbmw->Run();
delete pdir;
return 0;
}
#include"carbuilder.h"
carmodel::carmodel()
{
}
carmodel::~carmodel()
{
}
void carmodel::SetSequence(vector<string> *pSeq)
{
m_pSequence = pSeq;
}
void carmodel::Run()
{
vector<string>::const_iterator it = m_pSequence->begin();
for(;it != m_pSequence->end();++it )
{
string actionName = *it;
if(actionName.compare("start") == 0)
{
Start();
}
else if(actionName.compare("stop") == 0)
{
Stop();
}
else if(actionName.compare("alarm") == 0)
{
Alarm();
}
else if(actionName.compare("engine boom") == 0)
{
EngineBoom();
}
}
}
benmodel::benmodel()
{
}
benmodel::~benmodel()
{
}
void benmodel::Start()
{
cout<<"ben start..."<<endl;
}
void benmodel::Stop()
{
cout<<"ben stop..."<<endl;
}
void benmodel::Alarm()
{
cout<<"ben alarm..."<<endl;
}
void benmodel::EngineBoom()
{
cout<<"ben engineboom..."<<endl;
}
bmwmodel::bmwmodel()
{
}
bmwmodel::~bmwmodel()
{
}
void bmwmodel::Start()
{
cout<<"bmw start..."<<endl;
}
void bmwmodel::Stop()
{
cout<<"bmw stop..."<<endl;
}
void bmwmodel::Alarm()
{
cout<<"bmw alarm..."<<endl;
}
void bmwmodel::EngineBoom()
{
cout<<"bmw engineboom..."<<endl;
}
benbuilder::benbuilder()
{
//在構造函數中創建奔馳汽車的模型
pBen = new benmodel();
}
benbuilder::~benbuilder()
{
}
carmodel* benbuilder::GetCarModel()
{
return pBen;
}
void benbuilder::SetSequence(vector<string> *pSeq)
{
pBen->SetSequence(pSeq);
}
bmwbuilder::bmwbuilder()
{
//在構造函數中創建寶馬汽車的模型
pBmw = new bmwmodel();
}
bmwbuilder::~bmwbuilder()
{
}
carmodel* bmwbuilder::GetCarModel()
{
return pBmw;
}
void bmwbuilder::SetSequence(vector<string> *pSeq)
{
pBmw->SetSequence(pSeq);
}
director::director()
{
pBen = new benbuilder();
pBmw = new bmwbuilder();
pSequence = new vector<string>();
}
director::~director()
{
delete pBen;
delete pBmw;
delete pSequence;
}
benmodel * director::GetABenModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("stop");
pBen->SetSequence(pSequence);
return dynamic_cast<benmodel*>(pBen->GetCarModel());
}
benmodel * director::GetBBenModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("Alarm");
pSequence->push_back("Stop");
pBen->SetSequence(pSequence);
return dynamic_cast<benmodel*>(pBen->GetCarModel());
}
bmwmodel *director::GetCBmwModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("stop");
pBmw->SetSequence(pSequence);
return dynamic_cast<bmwmodel*>(pBmw->GetCarModel());
}
bmwmodel *director::GetDBmwModel()
{
pSequence->clear();
pSequence->push_back("start");
pSequence->push_back("Engineboom");
pBmw->SetSequence(pSequence);
return dynamic_cast<bmwmodel*>(pBmw->GetCarModel());
}
int main()
{
director *pdir = new director;
benmodel *pben = pdir->GetABenModel();
pben->Run();
bmwmodel *pbmw = pdir->GetCBmwModel();
pbmw->Run();
delete pdir;
return 0;
}
摘自 lbqBraveheart的專欄