设计模式:抽象工厂模式(4.2)C++版
来源:互联网 发布:sqlplus执行sql文件 -c 编辑:程序博客网 时间:2024/05/21 18:32
抽象工厂模式:创建一族相关的产品。
C++示例代码如下:
#include "stdafx.h"/** CONTENTS: DESIGN PATTERN, ABSTRACT FACTORY PATTERN* AUTHOR: YAO H. WANG* TIME: 2013-11-1 17:25:29* EDITION: 1** ALL RIGHTS RESERVED!*/#include <string>#include <list>#include <iostream>using namespace std;//调料class Dough{public:virtual void print() =0;};class ThickCrustDough: public Dough{public:void print(){cout << "ThickCrustDough" << endl;}};class ThinCrustDough: public Dough{public:void print(){cout << "ThinCrustDough" << endl;}};class Sauce{public:virtual void print() =0;};class PlumTomatoSauce: public Sauce{public:void print(){cout << "PlumTomatoSauce" << endl;}};class MarinaraSauce: public Sauce{public:void print(){cout << "MarinaraSauce" << endl;}};class Cheese{public:virtual void print() =0;};class MozzarellaCheese: public Cheese{public:void print(){cout << "MozzarellaCheese" << endl;}};class ReggianoCheese: public Cheese{public:void print(){cout << "ReggianoCheese" << endl;}};class Veggies{public:virtual void print() = 0;};class Garlic: public Veggies{public:void print(){cout << "Garlic" << endl;}};class Onion: public Veggies{public:void print(){cout << "Onion" << endl;}};class Mushroom: public Veggies{public:void print(){cout << "Mushroom" << endl;}};class RedPepper: public Veggies{public:void print(){cout << "RedPepper" << endl;}};class BlackOlives: public Veggies{public:void print(){cout << "BlackOlives" << endl;}};class Spinach: public Veggies{public:void print(){cout << "Spinach" << endl;}};class EggPlant: public Veggies{public:void print(){cout << "EggPlant" << endl;}};class Pepperoni{virtual void print() = 0;};class SlicePepperoni: public Pepperoni{void print(){cout << "SlicePepperoni" << endl;}};class Clams{public:virtual void print() = 0;};class FrozenClams: public Clams{public:void print(){cout << "FrozenClams" << endl;}};class FreshClams: public Clams{public:void print(){cout << "FreshClams" << endl;}};//调料工厂class PizzaIngredientFactory{public:virtual Dough* createDough() = 0;virtual Sauce* createSauce() = 0;virtual Cheese* createCheese() = 0;virtual list<Veggies*> createVeggies() = 0;virtual Pepperoni* createPepperoni() = 0;virtual Clams* createClams() = 0;};class NYPizzaIngredientFactory: public PizzaIngredientFactory{public:Dough* createDough(){return new ThinCrustDough();}Sauce* createSauce(){return new MarinaraSauce();}Cheese* createCheese(){return new ReggianoCheese();}list<Veggies*> createVeggies(){list<Veggies*> lv;lv.push_back(new Garlic());lv.push_back(new Onion());lv.push_back(new Mushroom());lv.push_back(new RedPepper());return lv;}Pepperoni* createPepperoni(){return new SlicePepperoni();}Clams* createClams(){return new FreshClams();}};class ChicagoPizzaIngredientFactory: public PizzaIngredientFactory{public:Dough* createDough(){return new ThickCrustDough();}Sauce* createSauce(){return new PlumTomatoSauce();}Cheese* createCheese(){return new MozzarellaCheese();}list<Veggies*> createVeggies(){list<Veggies*> lv;lv.push_back(new BlackOlives());lv.push_back(new Spinach());lv.push_back(new EggPlant());return lv;}Pepperoni* createPepperoni(){return new SlicePepperoni();}Clams* createClams(){return new FrozenClams();}};//产品class Pizza{public:string name;Dough *dough;Sauce *sauce;list<Veggies*> lv ;Cheese *cheese;Pepperoni *pepperoni;Clams *clam;virtual void prepare() = 0;virtual void bake(){cout << "Bake for 25 minutes at 350" << endl;}virtual void cut(){cout << "Cutting the pizza into diagonal slices" << endl;}virtual void box(){cout << "Place pizza in official PizzaStore box" << endl;}void setName(string name){this->name = name;}string getName(){return name;}};class CheesePizza: public Pizza{private:PizzaIngredientFactory *ingredientFactory;public:CheesePizza(PizzaIngredientFactory *ingredientFactory){this->ingredientFactory = ingredientFactory;}void prepare(){cout << "Preparing " << getName() << endl;dough = ingredientFactory->createDough();sauce = ingredientFactory->createSauce();cheese = ingredientFactory->createCheese();}};class ClamPizza: public Pizza{private:PizzaIngredientFactory *ingredientFactory;public:ClamPizza(PizzaIngredientFactory *ingredientFactory){this->ingredientFactory = ingredientFactory;}void prepare(){cout << "Preparing " << getName() << endl;dough = ingredientFactory->createDough();sauce = ingredientFactory->createSauce();cheese = ingredientFactory->createCheese();clam = ingredientFactory->createClams();}};class VeggiePizza: public Pizza{private:PizzaIngredientFactory *ingredientFactory;public:VeggiePizza(PizzaIngredientFactory *ingredientFactory){this->ingredientFactory = ingredientFactory;}void prepare(){cout << "Preparing " << getName() << endl;dough = ingredientFactory->createDough();sauce = ingredientFactory->createSauce();cheese = ingredientFactory->createCheese();lv = ingredientFactory->createVeggies();}};class PepperoniPizza: public Pizza{private:PizzaIngredientFactory *ingredientFactory;public:PepperoniPizza(PizzaIngredientFactory *ingredientFactory){this->ingredientFactory = ingredientFactory;}void prepare(){cout << "Preparing " << getName() << endl;dough = ingredientFactory->createDough();sauce = ingredientFactory->createSauce();cheese = ingredientFactory->createCheese();pepperoni = ingredientFactory->createPepperoni();}};//工厂class PizzaStore{public:Pizza* orderPizza(string type){Pizza *pizza;pizza = createPizza(type);pizza->prepare();pizza->bake();pizza->cut();pizza->box();return pizza;}private:virtual Pizza* createPizza(string type) = 0;};class NYPizzaStore: public PizzaStore{Pizza* createPizza(string type){Pizza *pizza = NULL;PizzaIngredientFactory *ingredientFactory = new NYPizzaIngredientFactory();if("cheese" == type){pizza = new CheesePizza(ingredientFactory);pizza->setName("New York Style Cheese Pizza");}else if("veggie" == type){pizza = new VeggiePizza(ingredientFactory);pizza->setName("New York Style Veggie Pizza");}else if("clam" == type){pizza = new ClamPizza(ingredientFactory);pizza->setName("New York Style Clam Pizza");}else if("pepperoni" == type){pizza = new PepperoniPizza(ingredientFactory);pizza->setName("New York Style Pepperoni Pizza");}return pizza;}};class ChicagoPizzaStore: public PizzaStore{Pizza* createPizza(string type){Pizza *pizza = NULL;PizzaIngredientFactory *ingredientFactory = new ChicagoPizzaIngredientFactory();if("cheese" == type){pizza = new CheesePizza(ingredientFactory);pizza->setName("Chicago Style Cheese Pizza");}else if("veggie" == type){pizza = new VeggiePizza(ingredientFactory);pizza->setName("Chicago Style Veggie Pizza");}else if("clam" == type){pizza = new ClamPizza(ingredientFactory);pizza->setName("Chicago Style Clam Pizza");}else if("pepperoni" == type){pizza = new PepperoniPizza(ingredientFactory);pizza->setName("Chicago Style Pepperoni Pizza");}return pizza;}};//测试void main(){PizzaStore *nyStore = new NYPizzaStore();PizzaStore *chicagoStore = new ChicagoPizzaStore();Pizza *pizza = nyStore->orderPizza("cheese");cout << "Ethan ordered a " + pizza->getName() << endl;pizza = chicagoStore->orderPizza("veggie");cout << "Joel ordered a " + pizza->getName() << endl;delete nyStore;delete chicagoStore;}
- 设计模式:抽象工厂模式(4.2)C++版
- 设计模式的实现(C++)------抽象工厂模式(AbstractFactory)
- C语言和设计模式(抽象工厂模式)
- C语言和设计模式(抽象工厂模式)
- C语言和设计模式(抽象工厂模式)
- C语言和设计模式(抽象工厂模式)
- 18.C语言和设计模式(抽象工厂模式)
- 9、C语言和设计模式(抽象工厂模式)
- 设计模式之抽象工厂模式(c++)
- 设计模式(抽象工厂)
- 抽象工厂模式(C#)
- C#--抽象工厂设计模式原理
- C#--抽象工厂设计模式--三层框架
- 设计模式(4)-抽象工厂模式
- 设计模式:抽象工厂模式(Python)
- 设计模式(JAVA)------抽象工厂模式
- 设计模式(三)---抽象工厂模式
- 抽象工厂模式---设计模式(好)
- Word 2010如何从正文开始设置页码
- 【Android开发那点破事】打开APP加载页面实现
- Send 和 WSASend
- 键盘管理
- 给单选框赋值
- 设计模式:抽象工厂模式(4.2)C++版
- 获取网络数据中的数组显示成ListView的简单流程
- strtok
- 标准模板库(STL)学习探究之Multimap容器
- FTP文件上传下载删除_解决乱码.doc
- Java中的String思考。
- Word-break:break-word
- Ubuntu下安装字体的最简方法
- [FAILED]Marking disk "DATA02" as an ASM disk