建造者模式

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

建造者模式,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。一段晦涩难懂的文字,实现创建不同表示的方法就是给创建的过程传入创建的参数。详细的还是看代码吧。10.1.解释main(),客户CCarModel,产品模型CBenzModel,奔驰模型CBMWModel,宝马模型ICarBuilder,建造者接口CBenzBuilder,奔驰建造者CBMWBuilder,宝马建造者CDirector,导演说明:CCarModel实现模板方法,Builder负责开始建造产品。建造产品时,构建的顺序由Director或main决定。注意:建造者模式和抽象工厂非常类似。建造者更重视产品建造时的逻辑顺序,而抽象工厂更重视生产出不同型号的产品,抽象工厂不关心顺序。看代码://Builder.cpp#includestdafx.h#includeCarModel.h#includeBenzModel.h#includeBMWModel.h#includeBenzBuilder.h#includeBMWBuilder.h#includeDirector.h#includevector#includeiostreamusingstd::vector;usingstd::string;usingstd::cout;usingstd::endl;voidDoBenzRun()//没有使用模式时,需要把步骤一条一条的传入模型。{cout----------生成奔驰模型----------endl;CBenzModel*pBenz=newCBenzModel();vectorstringseq;seq.push_back(engineboom);//客户要求run的时候先发动引擎seq.push_back(start);//启动起来seq.push_back(stop);//开了一段就停下来pBenz-SetSequence(&seq);pBenz-Run();deletepBenz;}voidDoBuilder()//使用模式后,由benzBuilder和bmwBuilder来生成,并且使用同样的创建顺序。{cout----------用同一个顺序,生成模型----------endl;vectorstringseq;seq.push_back(engineboom);seq.push_back(start);seq.push_back(stop);CBenzBuilderbenzBuilder;benzBuilder.SetSequence(&seq);CBenzModel*pBenz=dynamic_castCBenzModel*(benzBuilder.GetCarModel());pBenz-Run();CBMWBuilderbmwBuilder;bmwBuilder.SetSequence(&seq);CBMWModel*pBmw=dynamic_castCBMWModel*(bmwBuilder.GetCarModel());pBenz-Run();}voidDoDirector()//使用指导者来封装创建的逻辑,把创建的顺序内聚在指导者类里面。{cout----------批量生成模型----------endl;CDirectordirector;//1W辆A类型的奔驰车for(inti=0;i2;i++)director.GetABenzModel()-Run();//100W辆B类型的奔驰车for(inti=0;i2;i++)director.GetBBenzModel()-Run();//1000W辆C类型的宝马车for(inti=0;i2;i++)director.GetCBMWModel()-Run();}int_tmain(intargc,_TCHAR*argv[]){DoBenzRun();DoBuilder();DoDirector();_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);_CrtDumpMemoryLeaks();return0;}//CarModel.h#pragmaonce#includevector#includeiostreamusingstd::vector;usingstd::string;classCCarModel{public:CCarModel(void);virtual~CCarModel(void);voidRun();voidSetSequence(vectorstring*pSeq);protected:virtualvoidStart()=0;virtualvoidStop()=0;virtualvoidAlarm()=0;virtualvoidEngineBoom()=0;private:vectorstring*m_pSequence;};//CarModel.cpp#includeStdAfx.h#includeCarModel.h#includevector#includeiostreamusingstd::vector;usingstd::string;CCarModel::CCarModel(void){}CCarModel::~CCarModel(void){}voidCCarModel::SetSequence(vectorstring*pSeq){m_pSequence=pSeq;}voidCCarModel::Run(){vectorstring::const_iteratorit=m_pSequence-begin();for(;itm_pSequence-end();++it){stringactionName=*it;if(actionName.compare(start)==0){Start();}elseif(actionName.compare(stop)==0){Stop();}elseif(actionName.compare(alarm)==0){Alarm();}elseif(actionName.compare(engineboom)==0){EngineBoom();}}}//BenzModel.h#pragmaonce#includecarmodel.hclassCBenzModel:publicCCarModel{public:CBenzModel(void);~CBenzModel(void);protected:voidStart();voidStop();voidAlarm();voidEngineBoom();};//BenzModel.cpp#includeStdAfx.h#includeBenzModel.h#includeiostreamusingstd::cout;usingstd::endl;CBenzModel::CBenzModel(void){}CBenzModel::~CBenzModel(void){}voidCBenzModel::Start(){cout奔驰发动...endl;}voidCBenzModel::Stop(){cout奔驰停车...endl;}voidCBenzModel::Alarm(){cout奔驰鸣笛endl;}voidCBenzModel::EngineBoom(){cout奔驰引擎声音是这样....endl;}//BMWModel.h#pragmaonce#includecarmodel.hclassCBMWModel:publicCCarModel{public:CBMWModel(void);~CBMWModel(void);protected:voidStart();voidStop();voidAlarm();voidEngineBoom();};//BMWModel.cpp#includeStdAfx.h#includeBMWModel.h#includeiostreamusingstd::cout;usingstd::endl;CBMWModel::CBMWModel(void){}CBMWModel::~CBMWModel(void){}voidCBMWModel::Start(){cout宝马发动...endl;}voidCBMWModel::Stop(){cout宝马停车...endl;}voidCBMWModel::Alarm(){cout宝马鸣笛endl;}voidCBMWModel::EngineBoom(){cout宝马引擎声音是这样....endl;}//ICarBuilder.h#pragmaonce#includeCarModel.h#includeiostream#includevectorusingstd::string;usingstd::vector;classICarBuilder{public:ICarBuilder(void){}virtual~ICarBuilder(void){}virtualvoidSetSequence(vectorstring*pseq)=0;virtualCCarModel*GetCarModel()=0;};//BenzBuilder.h#pragmaonce#includeicarbuilder.h#includeCarModel.h#includeiostream#includevectorusingstd::string;usingstd::vector;classCBenzBuilder:publicICarBuilder{public:CBenzBuilder(void);~CBenzBuilder(void);voidSetSequence(vectorstring*pSeq);CCarModel*GetCarModel();private:CCarModel*m_pBenz;};//BenzBuilder.cpp#includeStdAfx.h#includeBenzBuilder.h#includeBenzModel.hCBenzBuilder::CBenzBuilder(void){m_pBenz=newCBenzModel();}CBenzBuilder::~CBenzBuilder(void){deletem_pBenz;}voidCBenzBuilder::SetSequence(vectorstring*pSeq){m_pBenz-SetSequence(pSeq);}CCarModel*CBenzBuilder::GetCarModel(){returnm_pBenz;}//BMWBuilder.h#pragmaonce#includeicarbuilder.h#includeCarModel.h#includeiostream#includevectorusingstd::string;usingstd::vector;classCBMWBuilder:publicICarBuilder{public:CBMWBuilder(void);~CBMWBuilder(void);voidSetSequence(vectorstring*pSeq);CCarModel*GetCarModel();private:CCarModel*m_pBMW;};//BMWBuilder.cpp#includeStdAfx.h#includeBMWBuilder.h#includeBMWModel.hCBMWBuilder::CBMWBuilder(void){m_p

1 / 11
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功