工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个,工厂方法让类把实例化推迟到子类。
#ifndef __FACTORY_H
#define __FACTORY_H
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Pizza
{
private:
string name;
string dough;
string sauce;
protected:
vector<string> toppings;
public:
Pizza(string name, string dough, string sauce) :name(name), dough(dough), sauce(sauce){}
void prepare()
{
cout << "Preparing " << name << endl;
cout << "Tossing dough..." << endl;
cout << "Adding sauce..." << endl;
cout << "Adding toppings: ";
for (int i = 0; i < toppings.size(); i++)
cout << " " << toppings[i];
cout << endl;
}
void bake()
{
cout << "Bake for 25 minutes at 350" << endl;
}
virtual void cut()
{
cout << "Cutting the pizza into diagonal slices" << endl;
}
void box()
{
cout << "Place pizza in official PizzaStore box" << endl;
}
string getName()
{
return name;
}
};
class NYStyleCheesePizza :public Pizza
{
public:
NYStyleCheesePizza() :Pizza("NY Style Sauce and Cheese Pizza", "Thin Crust Dough", "Marinara Sauce")
{
toppings.push_back("Grated Reggiano Cheese");
}
};
class NYStyleClamPizza :public Pizza
{
public:
NYStyleClamPizza() :Pizza("NY Style Clam Pizza","Thin Crust Dough","Marinara Sauce")
{
toppings.push_back("Grated Reggiano Cheese");
toppings.push_back("Fresh Clams from Long Island Sound");
}
};
class NYStylePepperoniPizza :public Pizza
{
public:
NYStylePepperoniPizza() :Pizza("NY Style Pepperoni Pizza","Thin Crust Dough","Marinara Sauce")
{
toppings.push_back("Grated Reggiano Cheese");
toppings.push_back("Sliced Pepperoni");
toppings.push_back("Garlic");
toppings.push_back("Onion");
toppings.push_back("Mushrooms");
toppings.push_back("Red Pepper");
}
};
class NYStyleVeggiePizza :public Pizza
{
public:
NYStyleVeggiePizza() :Pizza("NY Style Sauce and Cheese Pizza", "Thin Crust Dough", "Marinara Sauce")
{
toppings.push_back("Grated Reggiano Cheese");
toppings.push_back("Garlic");
toppings.push_back("Onion");
toppings.push_back("Mushrooms");
toppings.push_back("Red Pepper");
}
};
class ChicagoStyleCheesePizza :public Pizza
{
public:
ChicagoStyleCheesePizza() :Pizza("Chicago Style Deep Dish Cheese Pizza", "Extra Thick Crust Dough", "Plum Tomato Sauce")
{
toppings.push_back("Shredded Mozzarella Cheese");
}
void cut()
{
cout << "Cutting the pizza into square slcies" << endl;
}
};
class ChicagoStyleClamPizza :public Pizza
{
public:
ChicagoStyleClamPizza() :Pizza("Chicago Style Clam Pizza","Extra Thick Crust Dough","Plum Tomato Sauce")
{
toppings.push_back("Shredded Mozzarella Cheese");
toppings.push_back("Frozen Clams from Chesapeake Bay");
}
void cut()
{
cout << "Cutting the pizza into square slcies" << endl;
}
};
class ChicagoStylePepperoniPizza :public Pizza
{
public:
ChicagoStylePepperoniPizza() :Pizza("Chicago Style Pepperoni Pizza","Extra Thick Crust Dough","Plum Tomato Sauce")
{
toppings.push_back("Shredded Mozzarella Cheese");
toppings.push_back("Black Olives");
toppings.push_back("Spinach");
toppings.push_back("Eggplant");
toppings.push_back("Sliced Pepperoni");
}
void cut()
{
cout << "Cutting the pizza into square slcies" << endl;
}
};
class ChicagoStyleVeggiePizza :public Pizza
{
public:
ChicagoStyleVeggiePizza() :Pizza("Chicago Deep Dish Veggie Pizza","Extra Thick Crust Dough","Plum Tomato Sauce")
{
toppings.push_back("Shredded Mozzarella Cheese");
toppings.push_back("Black Olives");
toppings.push_back("Spinach");
toppings.push_back("Eggplant");
}
void cut()
{
cout << "Cutting the pizza into square slcies" << endl;
}
};
class PizzaStore
{
private:
Pizza *pizza;
public:
Pizza* orderPizza(string type)
{
Pizza* pizza;
pizza = createPizza(type);
pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
return pizza;
}
virtual Pizza* createPizza(string type) = 0;
};
class NYPizzaStore :public PizzaStore
{
public:
Pizza* createPizza(string type)
{
if (type == "cheese")
return new NYStyleCheesePizza();
else if (type == "veggie")
return new NYStyleVeggiePizza();
else if (type == "clam")
return new NYStyleClamPizza();
else if (type == "pepperoni")
return new NYStylePepperoniPizza();
else
return NULL;
}
};
class ChicagoPizzaStore :public PizzaStore
{
public:
Pizza* createPizza(string type)
{
if (type == "cheese")
return new ChicagoStyleCheesePizza();
else if (type == "veggie")
return new ChicagoStyleVeggiePizza();
else if (type == "clam")
return new ChicagoStyleClamPizza();
else if (type == "pepperoni")
return new ChicagoStylePepperoniPizza();
else
return NULL;
}
};
#endif
#include"Factory.h"
int main()
{
PizzaStore *nyStore = new NYPizzaStore();
PizzaStore *chicagoStore = new ChicagoPizzaStore();
Pizza *pizza = nyStore->orderPizza("cheese");
cout << "Ethan ordered a " << pizza->getName() << endl;
pizza = chicagoStore->orderPizza("clam");
cout << "Joel ordered a " << pizza->getName() << endl;
}