Builder - 生成器

来源:互联网 发布:联通大数据公司招聘 编辑:程序博客网 时间:2024/05/16 06:00

 结构图:





通俗解释:MM最爱听的就是“我爱你”这句话了,见到不同地方的MM,要能够用她们的方言跟她说这句话哦,我有一个多种语言翻译机,上面每种语言都有一个按键,见到MM我只要按对应的键,它就能够用相应的语言说出“我爱你”这句话了,国外的MM也可以轻松搞掂,这就是我的“我爱你”builder。(这一定比美军在伊拉克用的翻译机好卖) 

建造模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示建造模式使得产品内部表象可以独立的变化,客户不必知道产品内部组成的细节。建造模式可以强制实行一种分步骤进行的建造过程。

相关模式:Abstract Factory 与B u i l d e r相似,因为它也可以创建复杂对象。主要的区别是B u i l d e r模式着重于一步步构造一个复杂对象。而Abstract Factory着重于多个系列的产品对象(简单的或是复杂的)。B u i l d e r在最后的一步返回产品,而对于Abstract Factory来说,产品是立即返回的。C o m p o s i t e通常是用B u i l d e r生成的。

可变方面:如何创建一个组合对象。

源码示例:

#include <iostream>using namespace std;class Lunch{public:Lunch(){};~Lunch(){};public:virtual void Createstaplefood(void){};  //主食virtual void CreatMousse(int nNum){};   //甜点virtual void CreatSoup(int nNum){};    //汤virtual int GetResult(void){return 0;};};class ChineseLunch: public Lunch{public:ChineseLunch(){};~ChineseLunch(){};public:virtual void Createstaplefood(void);  virtual void CreatSoup(int nNum);virtual int GetResult(void);};void ChineseLunch::Createstaplefood(){cout<<"Create Rice for ChineseLunch"<<endl;}void ChineseLunch::CreatSoup(int nNum){cout<<"Create "<<nNum<<" tomato Soup for ChineseLunch"<<endl;}int ChineseLunch::GetResult(void){cout<<"ChineseLunch be done"<<endl;return 0;}class UsLunch: public Lunch{public:UsLunch();~UsLunch();public:virtual void Createstaplefood(void);  virtual void CreatMousse(int nNum);   virtual void CreatSoup(int nNum);virtual int GetResult(void);};void UsLunch::Createstaplefood(){cout<<"Create Beaf for UsLunch"<<endl;}void UsLunch::CreatMousse(int nNum){cout<<"Create "<<nNum<<" ice cream  for UsLunch"<<endl;}void UsLunch::CreatSoup(int nNum){cout<<"Create "<<nNum<<" potato Soup for UsLunch"<<endl;}int UsLunch::GetResult(void){cout<<"UsLunch be done"<<endl;return 0;}class FranceLunch: public Lunch{public:FranceLunch();~FranceLunch();public:virtual void Createstaplefood(void);virtual void CreatMousse(int nNum);virtual int GetResult(void);};void FranceLunch::Createstaplefood(void){cout<<"Create Beaf for FranceLunch"<<endl;}void FranceLunch::CreatMousse(int nNum){cout<<"Create "<<nNum<<" ice cream  for FranceLunch"<<endl;}int FranceLunch::GetResult(void){cout<<"FranceLunch be done"<<endl;return 0;}class Cook{public:Cook();~Cook();public:int CreateThreemealsChildrenLunch(Lunch& lunch);int CreateFourmealsChildrenLunch(Lunch& lunch);int CreateAdultFranceLunch(Lunch& lunch);};Cook::Cook(){}Cook::~Cook(){}int Cook::CreateThreemealsChildrenLunch(Lunch& lunch){lunch.CreatMousse(1);lunch.Createstaplefood();lunch.CreatSoup(1);return lunch.GetResult();}int Cook::CreateFourmealsChildrenLunch(Lunch& lunch){lunch.CreatMousse(1);lunch.Createstaplefood();lunch.CreatSoup(1);return lunch.GetResult();}int Cook::CreateAdultFranceLunch(Lunch& lunch){lunch.Createstaplefood();lunch.CreatMousse(4);return lunch.GetResult();}int main(){Cook Join;ChineseLunch chineselunch;Join.CreateThreemealsChildrenLunch(chineselunch);Join.CreateFourmealsChildrenLunch(chineselunch);return 0;}


原创粉丝点击