文中先用简单工厂来创建了pizza,后来当Pizza店有了加盟店,并且需求发生变化时:
用简单工厂创建的话,就要创建创建NYPizzaFactory、ChicagoPizzaFactory、CaliforniaPizzaFactory等多个工厂:
Code
NYPizzaFactory nyFactory = new NYPizzaFactory(); // 创建具有纽约风味的Pizza工厂
PizzaStore nyStore = new PizzaStore(nyFactory); // 建立Pizza店,经营纽约风味的Pizza
nyStore.OrderPizza("cheese"); // 订购具有纽约风味的CheesePizza
.其它具体工厂
用工厂方法的话
Code
class Program
{
static void Main(string[] args)
{
PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();
Pizza pizza = nyStore.OrderPizza("cheese");
Console.WriteLine("Ethan ordered a " + pizza.GetName());
pizza = chicagoStore.OrderPizza("cheese");
Console.WriteLine("Joel orderd a " + pizza.GetName());
Console.ReadLine();
}
}
public class NYPizzaStore : PizzaStore
{
public override Pizza CreatePizza(String paramPizzaType)
{
Pizza pizza = null;
if (paramPizzaType.Equals("cheese"))
{
pizza = new NYStyleCheesePizza();
}
else if (paramPizzaType.Equals("greek"))
{
pizza = new NYStyleGreekPizza();
}
else if (paramPizzaType.Equals("pepperon"))
{
pizza = new NYStylePepperoniPizza();
}
return pizza;
}
}
public class ChicagoPizzaStore : PizzaStore
{
public override Pizza CreatePizza(String paramPizzaType)
{
Pizza pizza = null;
if (paramPizzaType.Equals("cheese"))
{
pizza = new ChicagoStyleCheesePizza();
}
else if (paramPizzaType.Equals("greek"))
{
pizza = new ChicagoStyleGreekPizza();
}
else if (paramPizzaType.Equals("pepperon"))
{
pizza = new ChicagoStylePepperoniPizza();
}
return pizza;
}
}
public abstract class PizzaStore
{
public abstract Pizza CreatePizza(String paramType);
public Pizza OrderPizza(String paramType)
{
Pizza pizza = CreatePizza(paramType);
pizza.Prepare();
pizza.Bake();
pizza.Cut();
pizza.Box();
return pizza;
}
}
public abstract class Pizza
{
protected string name; // 名称
protected string sauce; // 酱料类型
protected string daugh; // 面团类型
protected List<string> toppings = new List<string>(); // 一套佐料
public void Prepare()
{
Console.WriteLine("Preparing " + name);
Console.WriteLine("Tossing dough ");
Console.WriteLine("Adding sauce ");
Console.WriteLine("Adding toppings : ");
foreach (string str in toppings)
{
Console.WriteLine(" " + str);
}
}
public virtual void Bake()
{
Console.WriteLine("Bake for 25 minutes at 350 ");
}
public virtual void Cut()
{
Console.WriteLine("Cutting the pizza into diagonal slices ");
}
public virtual void Box()
{
Console.WriteLine("Place pizza in official PizzaStore box");
}
public String GetName()
{
return name;
}
}
public class ChicagoStyleCheesePizza :Pizza
{
public ChicagoStyleCheesePizza()
{
name = "Chicago Style Deep Dish Cheese Pizza";
daugh = "Extra Thick Crust Dough";
sauce = "Plum Tomato Sauce ";
toppings.Add("Shredded Mizzarella Cheese ");
}
public override void Cut()
{
Console.WriteLine("Cutting the pizza into squre slices "); // 芝加哥的Pizza切成方形
}
}
public class NYStyleCheesePizza:Pizza
{
public NYStyleCheesePizza()
{
name = "NY Style Sauce and Cheese Pizza";
daugh = "Thin Crust Dough";
sauce = "Marinara Sauce ";
toppings.Add("Grated Regggiano Cheese");
}
}
疑惑1:
文中说当需求改变时,简单工厂将不能满足需求,可改变的需求包括:
1)、有些加盟店采用自创的流程,比如烘烤的做法有些差异(Bake方法改变)、不要切片(Cut方法不要)、采用其他公司的盒子(Box方法改变)等。
2)、Pizza的种类有可能发生改变(或增加或减少)。
我觉得这些改变都是在Pizza类内部发生的,如
pizza.Prepare();
pizza.Bake();
pizza.Cut();
pizza.Box();
可以在具体的pizza类中修改来实现,和采用哪种工厂没有什么关系吧?
疑惑2:
采用简单工厂时要创建具体的工厂:如 NYPizzaFactory nyFactory = new NYPizzaFactory();
采用工厂方法时也还是要创建具体的Pizza店,如:PizzaStore nyStore = new NYPizzaStore();
如果要增加加盟店,简单工厂需要增加一个具体的工厂,而工厂方法也需要增加一个具体的pizza店,客户端也还是要做相应的修改,看上去工厂方法比简单工厂好不了多少呀?