// 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;
}

设计模式入门,工厂模式,c++代码实现的更多相关文章

  1. 设计模式入门,策略模式,c++代码实现

    // test01.cpp : Defines the entry point for the console application.////第一章,设计模式入门,策略模式#include &quo ...

  2. 设计模式入门,命令模式,c++代码实现

    // test06.cpp : Defines the entry point for the console application.////设计模式第5章 命令模式#include "s ...

  3. 设计模式入门,单件模式,c++代码实现

    // test05.cpp : Defines the entry point for the console application.// #include "stdafx.h" ...

  4. 设计模式——抽象工厂模式及java实现

    设计模式--抽象工厂模式及java实现 设计模式在大型软件工程中很重要,软件工程中采用了优秀的设计模式有利于代码维护,方便日后更改和添加功能. 设计模式有很多,而且也随着时间在不断增多,其中最著名的是 ...

  5. 5. 星际争霸之php设计模式--抽象工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  6. 3. 星际争霸之php设计模式--简单工厂模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  7. iOS 设计模式之工厂模式

    iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...

  8. 设计模式之工厂模式(Factory)

    设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式 简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类 代码如下: #include <stdio.h> ...

  9. 浅析JAVA设计模式之工厂模式(一)

    1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...

随机推荐

  1. IE8引入JavaScript

    错误写法 : <script type="application/javascript"> 正确写法     <script type="text/ja ...

  2. JQuery实现全选、反选和取消功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. is 与 == 的区别;小数据池; 编码与解码

    1, is 与 == 的区别 ==  比较的是两边的值 is   比较的是两边的地址  id () 2,小数据池(在终端中) 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址 ...

  4. nginx处理高并发请求强于apache

    ginx 不同于 Apache2 的一点就是,Nginx 采用单线程,非阻塞,异步 IO 的工作模型. Apache2 对于每一个请求,都会创建一个新进程或线程,会浪费很多内存和 CPU 时间,而 N ...

  5. php 调用银联接口 【转载】

    首先要开启openssl开启方法为 openssl证书放在Apache的bin目录中. 其中的php_openssl.dll,ssleay32.dll,libeay32.dll,3个文件拷到windo ...

  6. XMPP使用tls 和sasl登录

    转自:http://ycool.com/post/xc98m5k 名词解释 TLS:安全传输层协议 TLS:Transport Layer Security 名词: 安全传输层协议(TLS)用于在两个 ...

  7. np.random.normal()

    高斯分布(Gaussian Distribution)的概率密度函数(probability density function): \[ f(x)=\frac1{\sqrt{2\pi}\sigma}\ ...

  8. js对函数参数的封装

    对函数参数的封装 一个原始函数有n个参数,用wrap对函数进行封装,生成一个新的函数,当给它传入参数数量为n的时候,将执行原始函数,否则不执行 //函数封装 function wrap(func){ ...

  9. mysql统计字段中某一字符串出现的次数

    (LENGTH(t.`range_00`) - LENGTH(REPLACE (t.`range_00`, "false", ""))) / LENGTH(&q ...

  10. V1-bug Alpha阶段项目展示

    V1-bug Alpha阶段项目展示 团队成员简介 Name Summary Sefie wxmwy V1-bug制造公司资深工程师精通各种抱大腿方式团队吉祥物 182 面面俱到流派一丝不苟 Powe ...