2/// EventFactory类
3/// </summary>
4public class EventFactory
5{
6 public EventLog Create()
7 {
8 return new EventLog();
9 }
10}
11/**//// <summary>
12/// FileFactory类
13/// </summary>
14public class FileFactory
15{
16 public FileLog Create()
17 {
18 return new FileLog();
19 }
20}
21
这两个工厂和具体的产品之间是平行的结构,并一一对应,并在它们的基础上抽象出一个公用的接口,结构图如下:
实现代码如下:
1/**//// <summary>
2/// LogFactory类
3/// </summary>
4public abstract class LogFactory
5{
6 public abstract Log Create();
7}
8
此时两个具体工厂的代码应该如下:
1/**//// <summary>
2/// EventFactory类
3/// </summary>
4public class EventFactory:LogFactory
5{
6 public override EventLog Create()
7 {
8 return new EventLog();
9 }
10}
11/**//// <summary>
12/// FileFactory类
13/// </summary>14public class FileFactory:LogFactory
15{
16 public override FileLog Create()
17 {
18 return new FileLog();
19 }
20}
21
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/