当前位置: 首页 > news >正文

设计模式入门,工厂模式,c++代码实现

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


转载于:https://www.cnblogs.com/wangting235/p/7111033.html

相关文章:

  • 线缆企业+缆360成标配 天津线缆展看产业发展未来
  • 希捷硬盘扩容软件-----DiscWizard
  • 拥抱开源!解析IBM Power8与Hadoop的不解之缘
  • 家庭布线需谨慎,老司机带你避开十大雷区
  • 报告称Flash视频格式2年内彻底消失
  • 阿里云核心产品再次降价:最高降幅35%
  • 网络运营者是个人信息保护的第一责任人
  • 苹果和亚马逊加入富士康对东芝存储芯片业务的竞购
  • 《Oracle高性能自动化运维》一一3.4 Redo优化
  • 光伏技术“质变期”是否已经到来?
  • 智慧城市建设快速发展 难题颇多亟待解决
  • H5公司升级改造其在克利夫兰的数据中心
  • 使用Apache Jmeter进行并发压力测试
  • 理解和使用SQL Server中的并行
  • 三类传感器助力保证无人驾驶安全
  • Docker容器管理
  • JS数组方法汇总
  • MySQL几个简单SQL的优化
  • PHP那些事儿
  • Python连接Oracle
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 简析gRPC client 连接管理
  • 如何解决微信端直接跳WAP端
  • 运行时添加log4j2的appender
  • 主流的CSS水平和垂直居中技术大全
  • 容器镜像
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​香农与信息论三大定律
  • #HarmonyOS:Web组件的使用
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • (+4)2.2UML建模图
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (C#)获取字符编码的类
  • (pojstep1.3.1)1017(构造法模拟)
  • (亲测有效)解决windows11无法使用1500000波特率的问题
  • (转)平衡树
  • *2 echo、printf、mkdir命令的应用
  • .NET Core 中的路径问题
  • .net framework4与其client profile版本的区别
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道
  • .net解析传过来的xml_DOM4J解析XML文件
  • .NET运行机制
  • .Net组件程序设计之线程、并发管理(一)
  • @RequestMapping用法详解
  • [ 代码审计篇 ] 代码审计案例详解(一) SQL注入代码审计案例
  • [BeginCTF]真龙之力
  • [C++]四种方式求解最大子序列求和问题
  • [C++提高编程](三):STL初识
  • [IDF]摩斯密码
  • [IE9] 解决了傲游、搜狗浏览器在IE9下网页截图的问题
  • [JS] 常用正则表达式集(一)
  • [NOSQL] Redis介绍
  • [PAT] 1041 Be Unique (20 分)Java
  • [Rust] 使用vscode实现HelloWorld程序并进行debug