抽象工厂模式(AbstractFactory)+反射技术

麦当劳(McDonalds)和肯德基(KFC)快餐店都经营汉堡(Hamburg)和可乐(Cola),用C#控制台应用程序实现这两个快餐店经营产品的抽象工厂模式。

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace AbstractFactory
 7 {
 8     //汉堡接口
 9     interface IHamburg
10     {
11     }
12     //KFC汉堡
13     class KFCHamburg : IHamburg
14     {
15         public KFCHamburg()
16         {
17             Console.WriteLine("KFC卖出了一份汉堡!");
18         }
19     }
20     //MD汉堡
21     class MDHamburg : IHamburg
22     {
23         public MDHamburg()
24         {
25             Console.WriteLine("McDonalds卖出了一份汉堡!");
26         }
27     }
28     //可乐接口
29     interface ICola
30     {
31     }
32     //KFC可乐
33     class KFCCola : ICola
34     {
35         public KFCCola()
36         {
37             Console.WriteLine("KFC卖出了一份可乐!");
38         }
39     }
40     //MD可乐
41     class MDCola : ICola
42     {
43         public MDCola()
44         {
45             Console.WriteLine("McDonalds卖出了一份可乐!");
46         }
47     }
48     //快餐店接口
49     interface INoshery
50     {
51         ICola CreateCola();
52         IHamburg CreateHamburg();
53     }
54     //KFC快餐店
55     class KFCNoshery : INoshery
56     {
57         public ICola CreateCola()
58         {
59             return new KFCCola();
60         }
61         public IHamburg CreateHamburg()
62         {
63             return new KFCHamburg();
64         }
65     }
66     //MD快餐店
67     class MDNoshery : INoshery
68     {
69         public ICola CreateCola()
70         {
71             return new MDCola();
72         }
73         public IHamburg CreateHamburg()
74         {
75             return new MDHamburg();
76         }
77     }
78     class Program
79     {
80         static void Main(string[] args)
81         {
82             INoshery noshery = new KFCNoshery();
83             ICola cola = noshery.CreateCola();
84             IHamburg hamburg = noshery.CreateHamburg();
85             noshery = new MDNoshery();
86             cola = noshery.CreateCola();
87             hamburg = noshery.CreateHamburg();
88         }
89     }
90 }

 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Reflection;//使用反射技术
 6 
 7 namespace AbstractFactory
 8 {
 9     //汉堡接口
10     interface IHamburg
11     {
12     }
13     //KFC汉堡
14     class KFCHamburg : IHamburg
15     {
16         public KFCHamburg()
17         {
18             Console.WriteLine("KFC卖出了一份汉堡!");
19         }
20     }
21     //MD汉堡
22     class MDHamburg : IHamburg
23     {
24         public MDHamburg()
25         {
26             Console.WriteLine("McDonalds卖出了一份汉堡!");
27         }
28     }
29     //可乐接口
30     interface ICola
31     {
32     }
33     //KFC可乐
34     class KFCCola : ICola
35     {
36         public KFCCola()
37         {
38             Console.WriteLine("KFC卖出了一份可乐!");
39         }
40     }
41     //MD可乐
42     class MDCola : ICola
43     {
44         public MDCola()
45         {
46             Console.WriteLine("McDonalds卖出了一份可乐!");
47         }
48     }
49     //抽象工厂模式缺点:如果再增加一个产品的话,要根据工厂的数量增加同等数量的类,
50     //还要修改每个工厂类的代码。优化(1)使用简单工厂进行优化,即去掉工厂接口和具体工厂类,
51     //用一个类来代替,在这个类中使用switch语句进行判断,来满足不同的工厂生产不同的
52     //产品。优化(2)使用反射技术进行优化。
53     class Noshery
54     {
55         private string NosheryName;
56         private string AssemblyName;
57         public Noshery(string AssemblyName, string NosheryName)
58         {
59             this.AssemblyName = AssemblyName;
60             this.NosheryName = NosheryName;
61         }
62         public ICola CreateCola()
63         {
64             string className = AssemblyName + "." + NosheryName + "Cola";
65             return (ICola)Assembly.Load(AssemblyName).CreateInstance(className);
66         }
67         public IHamburg CreateHamburg()
68         {
69             string className = AssemblyName + "." + NosheryName + "Hamburg";
70             return (IHamburg)Assembly.Load(AssemblyName).CreateInstance(className);
71         }
72     }
73     class Program
74     {
75         static void Main(string[] args)
76         {
77             Noshery kfc = new Noshery("AbstractFactory","KFC");
78             kfc.CreateCola();
79             kfc.CreateHamburg();
80             Noshery md = new Noshery("AbstractFactory","MD");
81             md.CreateCola();
82             md.CreateHamburg();
83         }
84     }
85 }

 

 

posted @ 2012-05-14 21:55  ZH奶酪  阅读(668)  评论(0编辑  收藏  举报