[GoF设计模式]Builder模式和Facade模式的C++实现
2009-09-22 09:29
741 查看
昨天晚上寝室断网了,今天补发。
@【Builder模式】
Builder创建者模式要解决的问题是:当我们要创建的对象很复杂的时候(通常由很多其他的对象组合而成),我们要复杂对象的创建过 程和这个对象的表示分开来,这样做的好处是可以一步一步的得到复杂对象的构建,由于在每一步的构建过程中可以引入参数,使得经过相同的步骤创建最后可以得到的对象的展示是不一样的。
【图解】
【程序】
【输出】
@【Facade模式】
Facade模式-门面模式的作用是
(1)为复杂的系统提供一个简单的接口
(2)将子系统和调用者或称客户相分离
(3)层次化的结构,减少层之间的依赖。
【图解】以病人去门诊治疗子系统(只简单的列出一个子系统)为例:
【程序】
【输出】
@【Builder模式】
Builder创建者模式要解决的问题是:当我们要创建的对象很复杂的时候(通常由很多其他的对象组合而成),我们要复杂对象的创建过 程和这个对象的表示分开来,这样做的好处是可以一步一步的得到复杂对象的构建,由于在每一步的构建过程中可以引入参数,使得经过相同的步骤创建最后可以得到的对象的展示是不一样的。
【图解】
【程序】
]/******************************************************************* * * DESCRIPTION: VehicleBuilder Class ,[抽象创建者] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef VEHICLEBUILDER_H_ #define VEHICLEBUILDER_H_ class VehicleBuilder { public: /*constructor and destructor*/ VehicleBuilder(){} virtual ~VehicleBuilder(){} virtual void getVehicle()=0; //Assemble vehicle and deliver protected: /*create parts function*/ virtual void createFrame()=0;//Create Parts virtual void createChassis()=0; virtual void createTyre()=0; }; #endif
]/******************************************************************* * * DESCRIPTION: CarBuilder Class ,[具体创建者] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef CARBUILDER_H_ #define CARBUILDER_H_ /** include files **/ #include <iostream> #include "VehicleBuilder.h" #include "Car.h" using namespace std; class CarBuilder:public VehicleBuilder { public: /*constructor and destructor*/ CarBuilder() { this->_car=NULL; } virtual ~CarBuilder() { if(_car!=NULL) delete _car; } virtual void getVehicle() { cout<<"Car being built......"<<endl; this->createFrame(); this->createChassis(); this->createTyre(); cout<<"Car assembling......"<<endl; this->_car=new Car(); cout<<"*******Now, It can be diliveried!......"<<endl; return; } protected: /*interface function*/ virtual void createChassis() { cout<<"CarBuilder:Car chassis created!"<<endl; return; } virtual void createFrame() { cout<<"CarBuilder:Car frame created!"<<endl; return; } virtual void createTyre() { cout<<"CarBuilder:Car tyre created!"<<endl; return; } private: Car *_car; }; #endif
]/******************************************************************* * * DESCRIPTION: Trunck Builder, [具体创建者] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef TRUCKBUILDER_H_ #define TRUCKBUILDER_H_ /** include files **/ #include "VehicleBuilder.h" #include "Truck.h" class TruckBuilder:public VehicleBuilder { public: /*constructor and destructor*/ TruckBuilder() { this->_truck=NULL; } virtual ~TruckBuilder() { if(_truck!=NULL) delete _truck; } virtual void getVehicle() { cout<<"Truck being built......"<<endl; this->createFrame(); this->createChassis(); this->createTyre(); cout<<"Truck assembling......"<<endl; this->_truck=new Truck(); cout<<"*******Now, It can be diliveried!......"<<endl; return; } protected: /*create parts function*/ virtual void createChassis() { cout<<"TruckBuilder: Truck chassis created!"<<endl; return; } virtual void createFrame() { cout<<"TruckBuilder: Truck frame created!"<<endl; return; } virtual void createTyre() { cout<<"TruckBuilder: Truck tyre created!"<<endl; return; } private: Truck *_truck; }; #endif
]/******************************************************************* * * DESCRIPTION: Manager Class, the director of Builder[厂长/经理] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef MANAGER_H_ #define MANAGER_H_ /** include files **/ #include "VehicleBuilder.h" #include "CarBuilder.h" #include "TruckBuilder.h" class Manager { public: /*Constructor and destructor*/ Manager(VehicleBuilder *carBuilder,VehicleBuilder *truckBuilder) { this->_carBuilder=carBuilder; this->_truckBuilder=truckBuilder; } virtual ~Manager() { if(_carBuilder!=NULL)delete _carBuilder; if(_truckBuilder!=NULL)delete _truckBuilder; } /*Manager ask the car workshop to produce car*/ void AskBuilder_toProduceCar() { this->_carBuilder->getVehicle(); return; } /*Manager ask the truck workshop to produce truck*/ void AskBuilder_toProduceTruck() { this->_truckBuilder->getVehicle(); return; } private: /*The Mananger has 2 workshop(builder)*/ VehicleBuilder *_carBuilder; VehicleBuilder *_truckBuilder; }; #endif
]/******************************************************************* * * DESCRIPTION:Car Class [产品类] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef CAR_H_ #define CAR_H_ #include <iostream> using namespace std; class Car { public: Car() { cout<<"Car Now Completed!"<<endl; } virtual ~Car(){} }; #endif
]/******************************************************************* * * DESCRIPTION:Truck Class [产品类] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef TRUCK_H_ #define TRUCK_H_ #include <iostream> using namespace std; class Truck { public: Truck() { cout<<"Truck Now Completed!"<<endl; } virtual ~Truck(){} }; #endif
]/******************************************************************* * * DESCRIPTION:Builder Pattern [创建者模式] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #include <iostream> #include "Manager.h" #include "VehicleBuilder.h" #include "CarBuilder.h" #include "TruckBuilder.h" #include "Car.h" #include "Truck.h" using namespace std; int main (int argc, char *argv[]) { //Build the vehicle workshop VehicleBuilder *carBuilder=new CarBuilder(); VehicleBuilder *truckBuilder=new TruckBuilder(); //Here a manager comes to direct the workshops Manager *manager=new Manager(carBuilder,truckBuilder); cout<<"/n*******Now a client order a Car......"<<endl; manager->AskBuilder_toProduceCar();//manager ask the car builder to produce a car cout<<"/n*******Now a client order a Truck......"<<endl; manager->AskBuilder_toProduceTruck();//manager ask the truck builder to produce a truck return(0); }
【输出】
@【Facade模式】
Facade模式-门面模式的作用是
(1)为复杂的系统提供一个简单的接口
(2)将子系统和调用者或称客户相分离
(3)层次化的结构,减少层之间的依赖。
【图解】以病人去门诊治疗子系统(只简单的列出一个子系统)为例:
【程序】
]/******************************************************************* * * DESCRIPTION: FacadeWaiter Class [接待员类-门面类] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef FACADEWAITER_H_ #define FACADEWAITER_H_ #include <iostream> #include "Registe.h"//挂号 #include "Diagnose.h"//就诊 #include "Assay.h"//化验 #include "Medicine.h"//取药 using namespace std; class FacadeWaiter { public: /*constructor and destructor*/ FacadeWaiter() { this->_reg=new Registe(); this->_diag=new Diagnose(); this->_ass=new Assay(); this->_med=new Medicine(); } virtual ~FacadeWaiter() { if(this->_reg!=NULL)delete this->_reg; if(this->_diag!=NULL)delete this->_diag; if(this->_ass!=NULL)delete this->_ass; if(this->_med!=NULL)delete this->_med; } /*concreate functions*/ void ReceiveClient()//接待员接待顾客,帮助完成一系列手续 { /*挂号*/ this->_reg->start(); this->_reg->end(); /*就诊*/ this->_diag->start(); this->_diag->end(); /*化验*/ this->_ass->payBeforeAssay(); this->_ass->start(); this->_ass->end(); /*取药*/ this->_med->payBeforeMedicine(); this->_med->start(); this->_med->end(); return; } private: Registe *_reg; Diagnose *_diag; Assay *_ass; Medicine *_med; }; #endif
]/******************************************************************* * * DESCRIPTION: Registe Class [挂号] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef REGISTE_H_ #define REGISTE_H_ #include <iostream> using namespace std; class Registe { public: /*constructor and destructor*/ Registe(){} virtual ~Registe(){} /*concreate functions*/ void start() { cout<<"Registing[挂号]: Start......"<<endl; return; } void end() { cout<<"Registing[挂号]: Complete, Go to Diagnose[去诊断]......"<<endl; return; } }; #endif
]/******************************************************************* * * DESCRIPTION: Diagnose Class [诊断] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef DIAGNOSE_H_ #define DIAGNOSE_H_ #include <iostream> using namespace std; class Diagnose { public: /*constructor and destructor*/ Diagnose(){} virtual ~Diagnose(){} /*concreate functions*/ void start() { cout<<"Diagnosing[就诊]:Start......"<<endl; return; } void end() { cout<<"Diagnosing[就诊]:Complete,Go to Assay[去化验]......"<<endl; return; } }; #endif
]/******************************************************************* * * DESCRIPTION: Assay Class [化验] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef ASSAY_H_ #define ASSAY_H_ #include <iostream> using namespace std; class Assay { public: /*constructor and destructor*/ Assay(){} virtual ~Assay(){} /*concreate functions*/ void payBeforeAssay() { cout<<"Assay[化验]:Before assay,Pay[付款]......"<<endl; return; } void start() { cout<<"Assay[化验]:Start......"<<endl; return; } void end() { cout<<"Assay[化验]:Complete,Go to Medicine[去取药]......"<<endl; return; } }; #endif
]/******************************************************************* * * DESCRIPTION: Medicine Class [取药] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #ifndef MEDICINE_H_ #define MEDICINE_H_ #include <iostream> using namespace std; class Medicine { public: /*constructor and destrutor*/ Medicine(){} virtual ~Medicine(){} /*concreate functions*/ void payBeforeMedicine() { cout<<"Medicine[取药]:Before medicine,Pay[付款]......"<<endl; return; } void start() { cout<<"Medicine[取药]:Start......"<<endl; return; } void end() { cout<<"Medicine[取药]:Complete,Go Away subsystem[门诊治疗子系统]"<<endl; return; } }; #endif
]/******************************************************************* * * DESCRIPTION: Facade Pattern [门面模式] * * AUTHOR:Neesky * * DATE:2009-9-21 * *******************************************************************/ #include <iostream> #include "FacadeWaiter.h" using namespace std; int main (int argc, char *argv[]) { /*Now Create the subsystem of Facade*/ FacadeWaiter *facadewaiter=new FacadeWaiter(); /*Now a Client come here*/ cout<<"/nA client come to subsystem for his illness!"<<endl; facadewaiter->ReceiveClient(); return(0); }
【输出】
相关文章推荐
- 我所理解的设计模式(C++实现)——外观模式(Facade Pattern)
- 常见设计模式的解析和实现(C++)之三-Builder模式
- 设计模式之Builder模式 (C++实现)
- [GoF设计模式]Proxy模式和Strategy模式的C++实现
- 设计模式精解-GoF 23种设计模式解析附C++实现源码
- 设计模式的解析和实现(C++)之三-Builder模式
- 设计模式之Builder模式 (C++实现)
- 设计模式C++实现(6)——建造者模式(Builder)
- 设计模式C++实现(3)——建造者模式(Builder)
- 设计模式之 创建者(Builder)模式(C++实现)
- 设计模式C++实现:包装器外观模式(Wrapper Facade)
- 设计模式(三)之建造者模式(Builder) C++实现
- [GoF设计模式]Iterator模式和Singleton模式的C++实现
- gof设计模式——抽象工厂 c++实现
- gof设计模式——生成器c++实现
- [GoF设计模式]Interpreter模式和Memento模式的C++实现
- [Gof设计模式]简单工厂模式和工厂模式的C++实现
- 我所理解的设计模式(C++实现)——建造者模式(Builder Pattern)
- [GoF设计模式]Decorator模式和Observer模式的C++实现
- [GoF设计模式]Adapter模式和Chain Of Responsibility模式的C++实现