// test04.cpp : Defines the entry point for the console application.
//
//设计模式第4章 工厂模式
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Pizza
{
public:
    string name;
    string dough;
    string sauce;
    vector<string> toppings;

void prepare()
    {
        cout<<"Preparing"<<name<<endl;
        cout<<"Tossing dough..."<<endl;
        cout<<"Adding sauce..."<<endl;
        cout<<"Adding toppings:"<<endl;
        for (int i = 0;i < toppings.size();i++)
        {
            cout<<toppings[i]<<endl;
        }
    }

void bake()
    {
        cout<<"Bake for 25 minutes at 350"<<endl;
    }

virtual void cut()
    {
        cout<<"Cutting ths pizza into diagonal slices"<<endl;
    }

void box()
    {
        cout<<"Please pizza in official PizzaStone box"<<endl;
    }

string getName()
    {
        return name;
    }
};

class PizzaStore
{
public:
    Pizza* orderPizza(string type)
    {
        Pizza* pizza;
        pizza = createPizza(type);

pizza->prepare();
        pizza->bake();
        pizza->cut();
        pizza->box();

return pizza;
    }

protected:
    virtual Pizza* createPizza(string type){return NULL;};//工厂模式的核心
};

class NYStyleCheesePizza : public Pizza
{
public:
    NYStyleCheesePizza(){
        name = "NY Style Sauce and Cheese Pizza";
        dough = "Thin Crust Dough";
        sauce = "Marinara Sauce";

toppings.push_back("Grated Reggiano Cheese");//上面覆盖的是意大利reggiano高级干酪
    }
};

class ChicagoStyleCheesePizza : public Pizza
{
public:
    ChicagoStyleCheesePizza()
    {
        name = "Chicago Style Deep Dish Cheese Pizza";
        dough = "Extra Thick Crust Dough";
        sauce = "Plum Tomato Sauce";

toppings.push_back("Shredded Mozzarella Cheese");
    }

void cut()
    {
        cout<<"Cutting the pizza into square slices"<<endl;
    }
};

class NYPizzaStore : public PizzaStore
{
    Pizza* createPizza(string item)
    {
        if (item == "cheese")
        {
            return new NYStyleCheesePizza();
        }
        /*else if (item == "veggie")
        {
            return new NYStyleVeggiePizza();
        }
        else if (item == "clam")
        {
            return new NYStyleClamPizza();
        }
        else if (item == "pepperoni")
        {
            return new NYStylePepperoniPizza();
        }*/
        else
        {
            return NULL;
        }
    }
};
class ChicagoPizzaStore : public PizzaStore
{
    Pizza* createPizza(string item)
    {
        if (item == "cheese")
        {
            return new ChicagoStyleCheesePizza();
        }
        else
        {
            return NULL;
        }
    }
};

//还有一种抽象工厂模式
int _tmain(int argc, _TCHAR* argv[])
{

PizzaStore* nyStore = new NYPizzaStore();
    PizzaStore* chicagoStore = new ChicagoPizzaStore();

Pizza* pizza = nyStore->orderPizza("cheese");
    cout<<"Ethan ordered a "<<pizza->getName()<<endl;

pizza = chicagoStore->orderPizza("cheese");
    cout<<"Joel ordered a "<<pizza->getName()<<endl;
    return 0;
}

最新文章

  1. java线程池(newSingleThreadExecutor())小应用
  2. Jquery利用Iframe实现跨子域
  3. Cocoapods的安装
  4. 安装python爬虫scrapy踩过的那些坑和编程外的思考
  5. 基于ADL5317的雪崩光电二极管(APD)偏压控制/光功率监测电路的设计
  6. PagedList 分页
  7. android 从服务器上获取APK并下载安装
  8. ActiveMQ_Windows版本的安装部署
  9. 一个非常好用的框架-AngularJS(一)
  10. CentOS中配置SoftWareRaid磁盘冗余阵列
  11. Python之面向对象三
  12. input里面的submit鼠标按钮属性cursor
  13. 《JavaScript.DOM》读书笔记
  14. python+selenium测试
  15. 杭电ACM1285----确定比赛名次『拓扑排序』
  16. ES6入门之let、cont
  17. hdu1069线性dp
  18. Koa快速入门教程(一)
  19. ElasticSearch入门 :Windows下安装ElasticSearch
  20. Netty 中 IOException: Connection reset by peer 与 java.nio.channels.ClosedChannelException: null

热门文章

  1. 现代 JavaScript 框架存在的主要原因
  2. C#-★★函数★★
  3. Kettle 值映射
  4. 大数据-hadoop HA集群搭建
  5. 1-----Docker实例-安装Nginx
  6. 读取Cert格式证书的密钥
  7. thymeleaf常用语法
  8. 配置idea解决乱码
  9. ps中的常用功能与技巧
  10. WCF系列教程之WCF消息交换模式之单项模式