您的位置:首页 > 其它

抽象工厂模式(Abstract Factory Pattern)

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

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

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

PizzaIngredientFactory = AbstractFactory

NYPizzaIngredientFactory = ProductFactory1

ChicagoIngredientFactory = ProductFactory2

Dough = AbstractProductA

ThickCrustDough = ProductA1

ThinCrustDough = ProductA2

Sauce = AbstractProductB

......

");
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 { get; set; }
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: