抽象工厂模式(Abstract Factory Pattern)

      抽象工厂模式(Abstract Factory Pattern)是工厂方法模式(Factory Method Pattern)进化出的模式,在系统中用于完成“一系列相互依赖的对象”的创建工作,同时由于需求的变化,往往存在着更多系列对象的创建工作。

      下面实现该模式的代码,仍然以匹萨快餐店为例,在工厂方法模式的示例中,匹萨店有了许多分店,现在匹萨店的老板要考虑各分店的匹萨原料来源了。在示例中,匹萨的4个原料是“一系列相互依赖的对象”,假设需求的变化点是匹萨店的位置,每个匹萨分店都有一“系列”的原料,而匹萨原料个数(4个)基本不会改变。

      抽象工厂模式的结构图,在实现代码中的对应关系是:

      PizzaIngredientFactory = AbstractFactory

      NYPizzaIngredientFactory = ProductFactory1

      ChicagoIngredientFactory = ProductFactory2

      Dough = AbstractProductA

      ThickCrustDough = ProductA1

      ThinCrustDough = ProductA2

      Sauce = AbstractProductB

      ......

 

 

  1     class App
  2     {
  3         static void Main()
  4         {
  5             //在纽约的一家匹萨店,一位客人订购了一份干酪匹萨
  6               PizzaStore nyStore = new NYPizzaStore();
  7 
  8             Pizza nyPizza = nyStore.CreatePizza(PizzaType.干酪匹萨);
  9         }
 10     }
 11 
 12     #region 匹萨快餐店
 13      public abstract class PizzaStore
 14     {
 15         public abstract Pizza CreatePizza(PizzaType type);
 16     }
 17 
 18     /// <summary>
 19     /// 纽约匹萨店
 20     /// </summary>
 21     public class NYPizzaStore : PizzaStore
 22     {
 23         public override Pizza CreatePizza(PizzaType type)
 24         {
 25             Pizza pizza = null;
 26             PizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory();
 27             switch (type)
 28             {
 29                 case PizzaType.干酪匹萨:
 30                     pizza = new CheesePizza(pizzaIngredientFactory);
 31                     pizza.Name = "纽约式干酪匹萨";
 32                     break;
 33                 case PizzaType.蛤蜊匹萨:
 34                     pizza = new ClamPizza(pizzaIngredientFactory);
 35                     pizza.Name = "纽约式蛤蜊匹萨";
 36                     break;
 37                 default:
 38                     break;
 39             }
 40 
 41             if (pizza != null) pizza.Produce();
 42 
 43             return pizza;
 44         }
 45     }
 46 
 47     /// <summary>
 48     /// 芝加哥匹萨店
 49     /// </summary>
 50     public class ChicagoPizzaStore : PizzaStore
 51     {
 52         public override Pizza CreatePizza(PizzaType type)
 53         {
 54             Pizza pizza = null;
 55             PizzaIngredientFactory pizzaIngredientFactory = new ChicagoIngredientFactory();
 56             switch (type)
 57             {
 58                 case PizzaType.干酪匹萨:
 59                     pizza = new CheesePizza(pizzaIngredientFactory);
 60                     pizza.Name = "芝加哥式干酪匹萨";
 61                     break;
 62                 case PizzaType.蛤蜊匹萨:
 63                     pizza = new ClamPizza(pizzaIngredientFactory);
 64                     pizza.Name = "芝加哥式蛤蜊匹萨";
 65                     break;
 66                 default:
 67                     break;
 68             }
 69 
 70             if (pizza != null) pizza.Produce();
 71 
 72             return pizza;
 73         }
 74     }
 75     #endregion
 76 
 77     #region 匹萨
 78      public enum PizzaType
 79     {
 80         干酪匹萨 = 1,
 81         蛤蜊匹萨 = 2
 82     }
 83 
 84     public abstract class Pizza
 85     {
 86         protected Dough dough;
 87         protected Sauce sauce;
 88         protected Cheese cheese;
 89         protected Clams clam;
 90 
 91         protected abstract void Prepare();
 92 
 93         protected virtual void Bake()
 94         {
 95             Console.WriteLine("350度烘烤25分钟");
 96         }
 97 
 98         protected virtual void Cut()
 99         {
100             Console.WriteLine("将匹萨切成尖角型");
101         }
102 
103         protected virtual void Box()
104         {
105             Console.WriteLine("将匹萨打包成商务餐装");
106         }
107 
108         public virtual void Produce()
109         {
110             Prepare();
111             Bake();
112             Cut();
113             Box();
114         }
115 
116         public string Name { getset; }
117     }
118 
119     public class CheesePizza : Pizza
120     {
121         PizzaIngredientFactory factory = null;
122         public CheesePizza(PizzaIngredientFactory factory)
123         {
124             this.factory = factory;
125         }
126 
127         protected override void Prepare()
128         {
129             this.dough = factory.CreateDough();
130             this.sauce = factory.CreateSauce();
131             this.cheese = factory.CreateCheese();
132 
133             Console.WriteLine("准备做一个 {0}", Name);
134             Console.WriteLine("制作匹萨面皮");
135             Console.WriteLine("添加调味汁");
136             Console.WriteLine("添加表面材料");
137         }
138     }
139 
140     public class ClamPizza : Pizza
141     {
142         PizzaIngredientFactory factory = null;
143         public ClamPizza(PizzaIngredientFactory factory)
144         {
145             this.factory = factory;
146         }
147 
148         protected override void Prepare()
149         {
150             this.dough = factory.CreateDough();
151             this.sauce = factory.CreateSauce();
152             this.clam = factory.CreateClams();
153 
154             Console.WriteLine("准备做一个 {0}", Name);
155             Console.WriteLine("制作匹萨面皮");
156             Console.WriteLine("添加调味汁");
157             Console.WriteLine("添加表面材料");
158         }
159     }
160     #endregion
161 
162     #region 匹萨原料工厂
163 
164     public interface PizzaIngredientFactory
165     {
166         Dough CreateDough();
167         Sauce CreateSauce();
168         Cheese CreateCheese();
169         Clams CreateClams();
170     }
171 
172     public class NYPizzaIngredientFactory : PizzaIngredientFactory
173     {
174         public Dough CreateDough()
175         {
176             //纽约人做匹萨都用薄皮面团
177             return new ThinCrustDough();
178         }
179 
180         public Sauce CreateSauce()
181         {
182             //纽约人喜欢意式沙司
183             return new MarinaraSauce();
184         }
185 
186         public Cheese CreateCheese()
187         {
188             //纽约人喜欢乳酪奶酪
189             return new ReggianoCheese();
190         }
191 
192         public Clams CreateClams()
193         {
194             //纽约人喜欢生鲜蛤蜊
195             return new FreshClams();
196         }
197     }
198 
199     public class ChicagoIngredientFactory : PizzaIngredientFactory
200     {
201         public Dough CreateDough()
202         {
203             //芝加哥人做匹萨都用厚皮面团
204             return new ThickCrustDough();
205         }
206 
207         public Sauce CreateSauce()
208         {
209             //芝加哥人喜欢番茄沙司
210             return new PlumTomatoSauce();
211         }
212 
213         public Cheese CreateCheese()
214         {
215             //芝加哥人喜欢意式奶酪
216             return new MozzarellaCheese();
217         }
218 
219         public Clams CreateClams()
220         {
221             //芝加哥人喜欢冰冻蛤蜊
222             return new FrozenClams();
223         }
224     }
225     #endregion
226 
227     #region 匹萨使用的原料
228 
229     public interface Dough
230     {
231         string PizzaDough();
232     }
233 
234     public class ThickCrustDough : Dough
235     {
236         public string PizzaDough()
237         {
238             return "厚皮面团";
239         }
240     }
241 
242     public class ThinCrustDough : Dough
243     {
244         public string PizzaDough()
245         {
246             return "薄皮面团";
247         }
248     }
249     
250     public interface Sauce
251     {
252         string PizzaSauce();
253     }
254 
255     public class PlumTomatoSauce : Sauce
256     {
257         public string PizzaSauce()
258         {
259             return "番茄沙司";
260         }
261     }
262 
263     public class MarinaraSauce : Sauce
264     {
265         public string PizzaSauce()
266         {
267             return "意式沙司";
268         }
269     }
270     
271     public interface Cheese
272     {
273         string PizzaTopCheese();
274     }
275 
276     public class MozzarellaCheese : Cheese
277     {
278         public string PizzaTopCheese()
279         {
280             return "意式奶酪";
281         }
282     }
283 
284     public class ReggianoCheese : Cheese
285     {
286         public string PizzaTopCheese()
287         {
288             return "乳酪奶酪";
289         }
290     }
291 
292     public interface Clams
293     {
294         string PizzaTopClams();
295     }
296 
297     public class FrozenClams : Clams
298     {
299         public string PizzaTopClams()
300         {
301             return "冰冻蛤蜊";
302         }
303     }
304 
305     public class FreshClams : Clams
306     {
307         public string PizzaTopClams()
308         {
309             return "生鲜蛤蜊";
310         }
311     }
312     #endregion

 

posted @ 2009-07-12 23:16  binfen  阅读(284)  评论(0编辑  收藏  举报