标签: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;
}
标签:ice protect bak amp ack prot ssi iostream tor
原文地址:http://www.cnblogs.com/wangting235/p/7111033.html