假设如果我们要出国,当然要先递交申请给出国办事处,然后由中国办事处再转交给你想去的国家的大使馆,让他们审核你的信息。如果用工厂模式可以这么做。
1 public abstract class Office
2 {
3 public abstract void CheckInfo();
4 }
5
6 public class USAoffice : Office
7 {
8 public override void CheckInfo()
9 { Console.WriteLine("This ia USAoffice CheckInfo"); }
10 }
11
12 public class UKoffice : Office
13 {
14 public override void CheckInfo()
15 { Console.WriteLine("This ia UKoffice CheckInfo"); }
16
17 }
18
19 public abstract class Creator
20 {
21 public abstract Office factory();
22 }
23
24 public class USAcreator : Creator
25 {
26 public override Office factory()
27 { return new USAoffice(); }
28 }
29
30 public class UKCreator : Creator
31 {
32 public override Office factory()
33 { return new UKoffice(); }
34 }
35
36 public class Client
37 {
38 public static void Main()
39 {
40 Creator c1 = new USAcreator();
41 Creator c2 = new UKCreator();
42
43 Office l1 = c1.factory();
44 Office l2 = c2.factory();
45
46 l1.CheckInfo();
47
48 Console.WriteLine("-----------------");
49
50 l2.CheckInfo();
51 }
52 }
2 {
3 public abstract void CheckInfo();
4 }
5
6 public class USAoffice : Office
7 {
8 public override void CheckInfo()
9 { Console.WriteLine("This ia USAoffice CheckInfo"); }
10 }
11
12 public class UKoffice : Office
13 {
14 public override void CheckInfo()
15 { Console.WriteLine("This ia UKoffice CheckInfo"); }
16
17 }
18
19 public abstract class Creator
20 {
21 public abstract Office factory();
22 }
23
24 public class USAcreator : Creator
25 {
26 public override Office factory()
27 { return new USAoffice(); }
28 }
29
30 public class UKCreator : Creator
31 {
32 public override Office factory()
33 { return new UKoffice(); }
34 }
35
36 public class Client
37 {
38 public static void Main()
39 {
40 Creator c1 = new USAcreator();
41 Creator c2 = new UKCreator();
42
43 Office l1 = c1.factory();
44 Office l2 = c2.factory();
45
46 l1.CheckInfo();
47
48 Console.WriteLine("-----------------");
49
50 l2.CheckInfo();
51 }
52 }
这是我参考吕老师的工厂模式的设计,他的文章写得很好,但这个设计并不能满足我的需求,因为这个模式在Client需要手动实例一个对象,如在Client端Creator c1 = new USAcreator();。所以我自己改良一下。
1
2 public abstract class Office
3 {
4 public abstract void CheckInfo();
5 }
6
7 public class USAoffice : Office
8 {
9 public override void CheckInfo()
10 { Console.WriteLine("This ia USAoffice CheckInfo"); }
11 }
12
13 public class UKoffice : Office
14 {
15 public override void CheckInfo()
16 { Console.WriteLine("This ia UKoffice CheckInfo"); }
17
18 }
19 public class Creator
20 {
21 public static Office Factory(string OfficeName)
22 {
23 if (OfficeName == "USA")
24 {
25 return new USAoffice();
26 }
27 if (OfficeName == "UK")
28 {
29 return new UKoffice();
30 }
31 }
32 }
33 public class Client
34 {
35 public static void Main()
36 {
37 string OfficeName = Console.ReadLine();
38 Office ConcreteOffice= Creator.Factory(OfficeName);
39
40 ConcreteOffice.CheckInfo();
41
42 }
43 }
2 public abstract class Office
3 {
4 public abstract void CheckInfo();
5 }
6
7 public class USAoffice : Office
8 {
9 public override void CheckInfo()
10 { Console.WriteLine("This ia USAoffice CheckInfo"); }
11 }
12
13 public class UKoffice : Office
14 {
15 public override void CheckInfo()
16 { Console.WriteLine("This ia UKoffice CheckInfo"); }
17
18 }
19 public class Creator
20 {
21 public static Office Factory(string OfficeName)
22 {
23 if (OfficeName == "USA")
24 {
25 return new USAoffice();
26 }
27 if (OfficeName == "UK")
28 {
29 return new UKoffice();
30 }
31 }
32 }
33 public class Client
34 {
35 public static void Main()
36 {
37 string OfficeName = Console.ReadLine();
38 Office ConcreteOffice= Creator.Factory(OfficeName);
39
40 ConcreteOffice.CheckInfo();
41
42 }
43 }
这个好处就在factory通过传递参数能自动判断该创建哪个实例,Client取到OfficeName就可以自动判断并创建所需要的实例。当然一眼也可以看出这个设计的致命点,就是如果以后Office越来越多,Creator的factory函数将会臃肿,不过目前这个设计足以满足我的需求啊。
第一次设计,虽然不好,但也能完成任务,以后发现问题再重构,一口并不能吃成胖子。