[Gof设计模式]简单工厂模式和工厂模式的C++实现

【简单工厂模式】-Simple Factory Pattern

 

 图解:

 [Gof设计模式]简单工厂模式和工厂模式的C++实现_第1张图片

代码:(简单起见将类定义和类实现放在同一个头文件中)


/** * Pizza Class (Abstract Class) */ #ifndef PIZZA_H_ #define PIZZA_H_ class Pizza { public: /*Constructor and Destructor*/ Pizza(){} virtual ~Pizza(){} /*Concreate Functions*/ virtual void Prepare()=0; virtual void Bake()=0; virtual void Cut()=0; virtual void Box()=0; }; #endif 

 

/** * ChessPizza inherited from Pizza * */ #ifndef CHESSPIZZA_H_ #define CHESSPIZZA_H_ /*Include Files*/ #include <iostream> #include "Pizza.h" using namespace std; class ChessPizza:public Pizza { public: /*Constructor and Destructor*/ ChessPizza(){} virtual ~ChessPizza(){} /*Concreate Funcitons*/ virtual void Prepare() { cout<<"ChessPizza:Doing 1-prepare......"<<endl; return; } virtual void Bake() { cout<<"ChessPizza:Doing 2-bake......"<<endl; return; } virtual void Cut() { cout<<"ChessPizza:Doing 3-cut......"<<endl; return; } virtual void Box() { cout<<"ChessPizza:Doing 4-box......"<<endl; return; } }; #endif

 

/** * PotatoPizza inherited from Pizza * */ #ifndef POTATOPIZZA_H_ #define POTATOPIZZA_H_ /*Include Files*/ #include <iostream> #include "Pizza.h" using namespace std; class PotatoPizza:public Pizza { public: /*Constructor and Destructor*/ PotatoPizza(){} virtual ~PotatoPizza(){} /*Concreate Funcitons*/ virtual void Prepare() { cout<<"PotatoPizza:Doing 1-prepare......"<<endl; return; } virtual void Bake() { cout<<"PotatoPizza:Doing 2-bake......"<<endl; return; } virtual void Cut() { cout<<"PotatoPizza:Doing 3-cut......"<<endl; return; } virtual void Box() { cout<<"PotatoPizza:Doing 4-box......"<<endl; return; } }; #endif

 

/** * VeggiePizza inherited from Pizza * */ #ifndef VEGGIEPIZZA_H_ #define VEGGIEPIZZA_H_ /*Include Files*/ #include <iostream> #include "Pizza.h" using namespace std; class VeggiePizza:public Pizza { public: /*Constructor and Destructor*/ VeggiePizza(){} virtual ~VeggiePizza(){} /*Concreate Funcitons*/ virtual void Prepare() { cout<<"VeggiePizza:Doing 1-prepare......"<<endl; return; } virtual void Bake() { cout<<"VeggiePizza:Doing 2-bake......"<<endl; return; } virtual void Cut() { cout<<"VeggiePizza:Doing 3-cut......"<<endl; return; } virtual void Box() { cout<<"VeggiePizza:Doing 4-box......"<<endl; return; } }; #endif

 

/** * Simple Factory for Pizza ,SimplePizzaFactory Class * */ #ifndef SIMPLEPIZZAFACTORY_H_ #define SIMPLEPIZZAFACTORY_H_ /*Include Files*/ #include "Pizza.h" #include "ChessPizza.h" #include "PotatoPizza.h" #include "VeggiePizza.h" /*Be used for descripe the Pizaa*/ enum PizzaType{ CHESS=0, POTATO=1, VEGGIE=2 }; class SimplePizzaFactory { /*public Members*/ public: SimplePizzaFactory(){} virtual ~SimplePizzaFactory(){} //Create Pizza as strPizzaType enumtype Pizza* CreatePizza(PizzaType enumType) { Pizza *pPizza=NULL; switch(enumType) { /*Prototype Pattern*/ case 0: pPizza=new ChessPizza(); break; case 1: pPizza=new PotatoPizza(); break; case 2: pPizza=new VeggiePizza(); break; default:break; } /*Now start produce this pizza*/ pPizza->Prepare(); //Prepare for Pizza pPizza->Bake(); //Bake pizza pPizza->Cut(); //Cut pizza pPizza->Box(); //Box pizza return pPizza; } }; #endif

 

/** * PizzaStore Class ,call the SimplePizzaFactory Class * */ #ifndef PIZZASTORE_H_ #define PIZZASTORE_H_ /*Include Files*/ #include "Pizza.h" #include "ChessPizza.h" #include "VeggiePizza.h" #include "PotatoPizza.h" #include "SimplePizzaFactory.h" class PizzaStore { /*Public Members*/ public: PizzaStore(SimplePizzaFactory* SimFactory) { this->_factory=SimFactory; } virtual ~PizzaStore() { if(this->_factory!=NULL) delete _factory; } Pizza* OrderPizza(PizzaType pizaType) { cout<<"/n==There is a client order Pizza=="<<endl; Pizza *_pizza=this->_factory->CreatePizza(pizaType);/*Factory Create Pizza*/ cout<<"==========Pizza Prepared=========/n"<<endl; return _pizza; } /*Private Members*/ private: SimplePizzaFactory* _factory; }; #endif

 

/******************************************************************* * * DESCRIPTION:Simple Factory Pattern [简单工厂模式] * * AUTHOR:Neesky * * DATE:2009-9-17 * *******************************************************************/ /** include files **/ #include <iostream> using namespace std; #include "Pizza.h" #include "SimplePizzaFactory.h" #include "PizzaStore.h" int main (int argc, char *argv[]) { Pizza *pPizza=0; SimplePizzaFactory *pFactory=new SimplePizzaFactory(); PizzaStore *pStore=new PizzaStore(pFactory);/*Can call SimplePizzaFactory*/ /*Client order a ChessPizza*/ pPizza=pStore->OrderPizza(CHESS); delete pPizza; /*Client order a PotatoPizza*/ pPizza=pStore->OrderPizza(POTATO); delete pPizza; /*Client order a VeggiePizza*/ pPizza=pStore->OrderPizza(VEGGIE); delete pPizza; return(0); }


输出:

 

[Gof设计模式]简单工厂模式和工厂模式的C++实现_第2张图片

 

 

【工厂模式】-Factory Pattern

  图解:

 

 

[Gof设计模式]简单工厂模式和工厂模式的C++实现_第3张图片

 

 

   [Gof设计模式]简单工厂模式和工厂模式的C++实现_第4张图片

 

 

 


代码:

/** * Pizza Class (Abstract Class) */ #ifndef PIZZA_H_ #define PIZZA_H_ class Pizza { public: /*Constructor and Destructor*/ Pizza(){} virtual ~Pizza(){} /*Concreate Functions*/ virtual void Prepare()=0; virtual void Bake()=0; virtual void Cut()=0; virtual void Box()=0; }; #endif

 

NewYorkVeggiePizza.h
   NewYork
VeggiePizza.h 与下类似 :

/** * NewYorkChessPizza inherited from Pizza * */ #ifndef NEWYORKCHESSPIZZA_H_ #define NEWYORKCHESSPIZZA_H_ /*Include Files*/ #include <iostream> #include "Pizza.h" using namespace std; class NewYorkChessPizza:public Pizza { public: /*Constructor and Destructor*/ NewYorkChessPizza(){} virtual ~NewYorkChessPizza(){} /*Concreate Funcitons*/ virtual void Prepare() { cout<<"NewYorkChessPizza:Doing 1-prepare......"<<endl; return; } virtual void Bake() { cout<<"NewYorkChessPizza:Doing 2-bake......"<<endl; return; } virtual void Cut() { cout<<"NewYorkChessPizza:Doing 3-cut......"<<endl; return; } virtual void Box() { cout<<"NewYorkChessPizza:Doing 4-box......"<<endl; return; } }; #endif

 

ChicagoVeggiePizza.h
   ChicagoVeggiePizza.h 与下类似 :

/** * ChessPizza inherited from Pizza * */ #ifndef CHICAGOCHESSPIZZA_H_ #define CHICAGOCHESSPIZZA_H_ /*Include Files*/ #include <iostream> #include "Pizza.h" using namespace std; class ChicagoChessPizza:public Pizza { public: /*Constructor and Destructor*/ ChicagoChessPizza(){} virtual ~ChicagoChessPizza(){} /*Concreate Funcitons*/ virtual void Prepare() { cout<<"ChicagoChessPizza:Doing 1-prepare......"<<endl; return; } virtual void Bake() { cout<<"ChicagoChessPizza:Doing 2-bake......"<<endl; return; } virtual void Cut() { cout<<"ChicagoChessPizza:Doing 3-cut......"<<endl; return; } virtual void Box() { cout<<"ChicagoChessPizza:Doing 4-box......"<<endl; return; } }; #endif

 

/** * * PizzaStore Class(Abstract Class) ,call the Pizza * Class(Abstract Class) * */ #ifndef PIZZASTORE_H_ #define PIZZASTORE_H_ /*Include Files*/ #include "Pizza.h" /*Be used for descripe the Pizaa*/ enum PizzaType{ CHESS=0, POTATO=1, VEGGIE=2, }; class PizzaStore { /*Public Members*/ public: PizzaStore() { } virtual ~PizzaStore() { } virtual Pizza* OrderPizza(PizzaType pizaType)=0; virtual Pizza* CreatePizza(PizzaType pizaType)=0; }; #endif

 

/******************************************************************* * * DESCRIPTION:Chicago Pizza Store ,inherited from Pizza Store * * AUTHOR:Neesky * * DATE:2009-9-17 * *******************************************************************/ #ifndef CHICAGOPIZZASTORE_H_ #define CHICAGOPIZZASTORE_H_ /** include files **/ #include <iostream> #include "PizzaStore.h" #include "Pizza.h" #include "ChicagoChessPizza.h" #include "ChicagoPotatoPizza.h" #include "ChicagoVeggiePizza.h" using namespace std; class ChicagoPizzaStore:public PizzaStore { /*public members*/ public: ChicagoPizzaStore(){} virtual ~ChicagoPizzaStore(){} Pizza * CreatePizza(PizzaType pizaType) //The Store can produce pizza { Pizza *_pizza=NULL; switch(pizaType) { /*Prototype Pattern*/ case 0: _pizza=new ChicagoChessPizza(); break;/*Produce Chicago Taste*/ case 1: _pizza=new ChicagoPotatoPizza(); break; case 2: _pizza=new ChicagoVeggiePizza(); break; default:break; } /*Now start produce this pizza*/ _pizza->Prepare(); //Prepare for Pizza _pizza->Bake(); //Bake pizza _pizza->Cut(); //Cut pizza _pizza->Box(); //Box pizza return _pizza; } Pizza * OrderPizza(PizzaType pizaType) //The client can order pizza { cout<<"/n===There is a client order Pizza(Chicago Store)==="<<endl; Pizza *_pizza=this->CreatePizza(pizaType); /*Factory Create Pizza*/ cout<<"===================Pizza Prepared=================/n"<<endl; return _pizza; } }; #endif

 

/******************************************************************* * * DESCRIPTION:New York Pizza Store ,inherited from Pizza Store * * AUTHOR:Neesky * * DATE:2009-9-17 * *******************************************************************/ #ifndef NEWYORKPIZZASTORE_H_ #define NEWYORKPIZZASTORE_H_ /** include files **/ #include <iostream> #include "PizzaStore.h" #include "Pizza.h" #include "NewYorkChessPizza.h" #include "NewYorkPotatoPizza.h" #include "NewYorkVeggiePizza.h" using namespace std; class NewYorkPizzaStore:public PizzaStore { /*public members*/ public: NewYorkPizzaStore(){} virtual ~NewYorkPizzaStore(){} Pizza * CreatePizza(PizzaType pizaType) //The Store can produce pizza { Pizza *_pizza=NULL; switch(pizaType) { /*Prototype Pattern*/ case 0: _pizza=new NewYorkChessPizza(); break;/*Produce NewYork Taste*/ case 1: _pizza=new NewYorkPotatoPizza(); break; case 2: _pizza=new NewYorkVeggiePizza(); break; default:break; } /*Now start produce this pizza*/ _pizza->Prepare(); //Prepare for Pizza _pizza->Bake(); //Bake pizza _pizza->Cut(); //Cut pizza _pizza->Box(); //Box pizza return _pizza; } Pizza * OrderPizza(PizzaType pizaType) //The client can order pizza { cout<<"/n===There is a client order Pizza(NewYork Store)==="<<endl; Pizza *_pizza=this->CreatePizza(pizaType); /*Factory Create Pizza*/ cout<<"===================Pizza Prepared--------=========/n"<<endl; return _pizza; } }; #endif

 

/******************************************************************* * * DESCRIPTION:Factory Pattern [工厂模式] * * AUTHOR:Neesky * * DATE:2009-9-17 * *******************************************************************/ /** include files**/ #include <iostream> #include "ChicagoPizzaStore.h" #include "NewYorkPizzaStore.h" using namespace std; int main (int argc, char *argv[]) { Pizza *pPizza=0; /*Open Store in Chicago*/ PizzaStore *PizStore=new ChicagoPizzaStore(); pPizza=PizStore->OrderPizza(CHESS);//Client Order Pizza delete pPizza; pPizza=PizStore->OrderPizza(POTATO); delete pPizza; pPizza=PizStore->OrderPizza(VEGGIE); delete pPizza; delete PizStore;//Store IN CHICAGO CRUSH. /*Open Store in New York*/ PizStore=new NewYorkPizzaStore(); pPizza=PizStore->OrderPizza(CHESS);//Client Order Pizza delete pPizza; pPizza=PizStore->OrderPizza(POTATO); delete pPizza; pPizza=PizStore->OrderPizza(VEGGIE); delete pPizza; delete PizStore;//Store IN CHICAGO CRUSH. return(0); }

 

输出:

 

[Gof设计模式]简单工厂模式和工厂模式的C++实现_第5张图片

 


 

你可能感兴趣的:([Gof设计模式]简单工厂模式和工厂模式的C++实现)