AbstractFactory
-------------------------------------------------------------------------------------------------------
UML:
本图来自dofactory
--------------------------------------------------------------------------------------------------------
- declares an interface for operations that create abstract products
- 声明一个操作创建抽象产品接口
- implements the operations to create concrete product objects
- 实现具体的操作,以创建具体产品对象
- declares an interface for a type of product object
- 声明针对多种产品类型对象的接口
- defines a product object to be created by the corresponding concrete factory
- 产品对象的定义,由相应的具体工厂创建
- implements the AbstractProduct interface
- 实现了抽象产品接口
- uses interfaces declared by AbstractFactory and AbstractProduct classes
- 使用由抽象产品和抽象工厂声明的接口
--------------------------------------------------------------------------------------------------------
SAMPLE:
代码
1 /// <summary>
2 /// MainApp startup class for Structural
3 /// Abstract Factory Design Pattern.
4 /// </summary>
5 class MainApp
6 {
7 /// <summary>
8 /// Entry point into console application.
9 /// </summary>
10 public static void Main()
11 {
12 // Abstract factory #1
13 AbstractFactory factory1 = new ConcreteFactory1();
14 Client client1 = new Client(factory1);
15 client1.Run();
16
17 // Abstract factory #2
18 AbstractFactory factory2 = new ConcreteFactory2();
19 Client client2 = new Client(factory2);
20 client2.Run();
21
22 // Wait for user input
23 Console.ReadKey();
24 }
25 }
26
27 /// <summary>
28 /// The 'AbstractFactory' abstract class
29 /// </summary>
30 abstract class AbstractFactory
31 {
32 public abstract AbstractProductA CreateProductA();
33 public abstract AbstractProductB CreateProductB();
34 }
35
36
37
38 /// <summary>
39 /// The 'ConcreteFactory1' class
40 /// </summary>
41 class ConcreteFactory1 : AbstractFactory
42 {
43 public override AbstractProductA CreateProductA()
44 {
45 return new ProductA1();
46 }
47 public override AbstractProductB CreateProductB()
48 {
49 return new ProductB1();
50 }
51 }
52
53 /// <summary>
54 /// The 'ConcreteFactory2' class
55 /// </summary>
56 class ConcreteFactory2 : AbstractFactory
57 {
58 public override AbstractProductA CreateProductA()
59 {
60 return new ProductA2();
61 }
62 public override AbstractProductB CreateProductB()
63 {
64 return new ProductB2();
65 }
66 }
67
68 /// <summary>
69 /// The 'AbstractProductA' abstract class
70 /// </summary>
71 abstract class AbstractProductA
72 {
73 }
74
75 /// <summary>
76 /// The 'AbstractProductB' abstract class
77 /// </summary>
78 abstract class AbstractProductB
79 {
80 public abstract void Interact(AbstractProductA a);
81 }
82
83
84 /// <summary>
85 /// The 'ProductA1' class
86 /// </summary>
87 class ProductA1 : AbstractProductA
88 {
89 }
90
91 /// <summary>
92 /// The 'ProductB1' class
93 /// </summary>
94 class ProductB1 : AbstractProductB
95 {
96 public override void Interact(AbstractProductA a)
97 {
98 Console.WriteLine(this.GetType().Name +
99 " interacts with " + a.GetType().Name);
100 }
101 }
102
103 /// <summary>
104 /// The 'ProductA2' class
105 /// </summary>
106 class ProductA2 : AbstractProductA
107 {
108 }
109
110 /// <summary>
111 /// The 'ProductB2' class
112 /// </summary>
113 class ProductB2 : AbstractProductB
114 {
115 public override void Interact(AbstractProductA a)
116 {
117 Console.WriteLine(this.GetType().Name +
118 " interacts with " + a.GetType().Name);
119 }
120 }
121
122 /// <summary>
123 /// The 'Client' class. Interaction environment for the products.
124 /// </summary>
125 class Client
126 {
127 private AbstractProductA _abstractProductA;
128 private AbstractProductB _abstractProductB;
129
130 // Constructor
131 public Client(AbstractFactory factory)
132 {
133 _abstractProductB = factory.CreateProductB();
134 _abstractProductA = factory.CreateProductA();
135 }
136
137 public void Run()
138 {
139 _abstractProductB.Interact(_abstractProductA);
140 }
141 }