// 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. jxl获取excel中的合并的单元格(主要是方法介绍)

    Range[] rangeCells = sheet.getMergedCells();// 返回sheet中合并的单元格数组 for (Range r : rangeCells) {//对数组遍历拿 ...

  2. lexical or preprocessor issue file not found in Xcode

    The very last suggestion is all I needed to fix this issue. Close & re-open Xcode.

  3. java 使用idea将工程打成jar并创建成exe文件类型执行

    https://blog.csdn.net/weixin_38310965/article/details/80392767

  4. Educational Codeforces Round 58 (Rated for Div. 2)

    A. Minimum Integer 水 #include<bits/stdc++.h> #define clr(a,b) memset(a,b,sizeof(a)) using name ...

  5. makedown学习笔记(以后可能会用makedown写博客)

    学习手册 https://www.zybuluo.com/mdeditor?url=https%3A%2F%2Fwww.zybuluo.com%2Fstatic%2Feditor%2Fmd-help. ...

  6. helloweblogic 官方qq群欢迎加入!

    点击加入helloweblogic 官方qq群,大家一起进行中间件技术交流,问题交流,互相帮忙互相学习. 我的网易博客地址:http://fm928.blog.163.com 收到网易博客的邮件,以后 ...

  7. js复习知识点

    null和undefined具体在什么时候使用? 如果定义的变量准备用来保存对象,那么最好用将改变量初始化为null而不是其他值 如果未定义的值可以用空字符 undefined是null派生出来的,所 ...

  8. jquery jgrid filterToolBar beforeSearch 修改postData

    beforeSearch: function() { var posted_data = $("#mygrid").jqGrid('getGridParam,'postData') ...

  9. React.js 小书 Lesson22 - props.children 和容器类组件

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson22 转载请注明出处,保留原文链接和作者信息. 有一类组件,充当了容器的作用,它定义了一种外层 ...

  10. Visual Studio2017中如何让ADO.NET实体数据模型[EntityFramework]支持MariaDB&MySQL数据源

    近期由于工作需要,需要重新修改设计系统的ADO.NET实体数据模型.edmx文件中间,在完成实际中途遇到一些实际使用问题,特此记录. 1. Visual Studio 2017 无法以实体设计模式打开 ...