3. 抽象工厂模式
抽象工厂:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
1 namespace AbstractFactory
2 {
3 //日志
4 public abstract class Log
5 {
6 public abstract void WriteLog(Exception ex);
7 }
8 //中文日志
9 public class ChinaLog:Log
10 {
11 public override void WriteLog(Exception ex)
12 {
13 Console.WriteLine("中文异常信息:"+ex.GetException());
14 }
15 }
16 //英文日志
17 public class EnglishLog:Log
18 {
19
20 public override void WriteLog(Exception ex)
21 {
22 Console.WriteLine("The error is:"+ex.GetException());
23 }
24 }
25 //异常
26 public abstract class Exception
27 {
28 public abstract string GetException();
29 }
30 //中文异常
31 public class ChinaException:Exception
32 {
33 public override string GetException()
34 {
35 return "错误!";
36 }
37 }
38 //英文异常
39 public class EnglishException:Exception
40 {
41 public override string GetException()
42 {
43 return "error!";
44 }
45 }
46 //抽象工厂
47 public abstract class AbstractFactory
48 {
49 public abstract Log CreateLog();
50 public abstract Exception CreateException();
51
52 }
53 //中文抽象工厂
54 public class ChinaFactory:AbstractFactory
55 {
56 public override Log CreateLog()
57 {
58 return new ChinaLog();
59 }
60
61 public override Exception CreateException()
62 {
63 return new ChinaException();
64 }
65 }
66 //英文抽象工厂
67 public class EnglishFactory:AbstractFactory
68 {
69 public override Log CreateLog()
70 {
71 return new EnglishLog();
72 }
73
74 public override Exception CreateException()
75 {
76 return new EnglishException();
77 }
78 }
79 //客户端
80 class Program
81 {
82 static void Main(string[] args)
83 {
84 string assembly=ConfigurationManager.AppSettings["Assembly"];
85 string instance=ConfigurationManager.AppSettings["Instance"];
86 AbstractFactory factory = (AbstractFactory)Assembly.Load(assembly).CreateInstance(instance);
87 Exception ex = factory.CreateException();
88 Log log = factory.CreateLog();
89 log.WriteLog(ex);
90 Console.ReadLine();
91 }
92 }
93 }
94
95 <?xml version="1.0" encoding="utf-8" ?>
96 <configuration>
97 <appSettings>
98 <add key="Assembly" value="AbstractFactory"/>
99 <add key="Instance" value="AbstractFactory.EnglishFactory"/>
100 </appSettings>
101 </configuration>
2 {
3 //日志
4 public abstract class Log
5 {
6 public abstract void WriteLog(Exception ex);
7 }
8 //中文日志
9 public class ChinaLog:Log
10 {
11 public override void WriteLog(Exception ex)
12 {
13 Console.WriteLine("中文异常信息:"+ex.GetException());
14 }
15 }
16 //英文日志
17 public class EnglishLog:Log
18 {
19
20 public override void WriteLog(Exception ex)
21 {
22 Console.WriteLine("The error is:"+ex.GetException());
23 }
24 }
25 //异常
26 public abstract class Exception
27 {
28 public abstract string GetException();
29 }
30 //中文异常
31 public class ChinaException:Exception
32 {
33 public override string GetException()
34 {
35 return "错误!";
36 }
37 }
38 //英文异常
39 public class EnglishException:Exception
40 {
41 public override string GetException()
42 {
43 return "error!";
44 }
45 }
46 //抽象工厂
47 public abstract class AbstractFactory
48 {
49 public abstract Log CreateLog();
50 public abstract Exception CreateException();
51
52 }
53 //中文抽象工厂
54 public class ChinaFactory:AbstractFactory
55 {
56 public override Log CreateLog()
57 {
58 return new ChinaLog();
59 }
60
61 public override Exception CreateException()
62 {
63 return new ChinaException();
64 }
65 }
66 //英文抽象工厂
67 public class EnglishFactory:AbstractFactory
68 {
69 public override Log CreateLog()
70 {
71 return new EnglishLog();
72 }
73
74 public override Exception CreateException()
75 {
76 return new EnglishException();
77 }
78 }
79 //客户端
80 class Program
81 {
82 static void Main(string[] args)
83 {
84 string assembly=ConfigurationManager.AppSettings["Assembly"];
85 string instance=ConfigurationManager.AppSettings["Instance"];
86 AbstractFactory factory = (AbstractFactory)Assembly.Load(assembly).CreateInstance(instance);
87 Exception ex = factory.CreateException();
88 Log log = factory.CreateLog();
89 log.WriteLog(ex);
90 Console.ReadLine();
91 }
92 }
93 }
94
95 <?xml version="1.0" encoding="utf-8" ?>
96 <configuration>
97 <appSettings>
98 <add key="Assembly" value="AbstractFactory"/>
99 <add key="Instance" value="AbstractFactory.EnglishFactory"/>
100 </appSettings>
101 </configuration>
解决了对系列创建的问题,各个子类中可能存在相互依赖的关系,如上面:日志的输出类,依赖于异常返回类。
这个模式是假设子类不变的情况下使用的,即只有log和exception这两个字类的创建,不会再添加其它的字类,但是可以添加相应的系列如:JapanesseFactory(日文工厂)