設計模式入門,原廠模式,c++代碼實現

來源:互聯網
上載者:User

標籤:ice   protect   bak   amp   ack   prot   ssi   iostream   tor   

// 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++代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.