设计模式学习笔记(一):抽象工厂

定义:

    用于提供一个不需要指定具体的类就能去创建一系列相互关联的对象的接口。

UML图:

参与者:

这种设计模式的参与者:

1、抽象工厂(AbstractFactory):声明一个创建抽象产品操作的接口

2、具体工厂(ConcreteFactory):实现创建具体产品的操作方法

3、抽象产品(AbstractProduct):为产品类型对象声明的接口

4、产品(Product):1、实现AbstractProduct;2、定义将由ConcreteFactory创建的产品对象。

5、客户端(Client ):使用AbstractFactory和AbstractProduct类声明的接口

示例:

下面代码使用抽象工厂模式创建同一个层次的对象。对象创建过程被抽象出来了,在Client代码中没有使用对象的类。

1 class Program
2 {
3 /// <summary>
4
5 /// Entry point into console application.
6
7 /// </summary>
8  
9 public static void Main()
10 {
11
12 // Abstract factory #1
13  
14 AbstractFactory factory1 = new ConcreteFactory1();
15
16 Client client1 = new Client(factory1);
17
18 client1.Run();
19
20
21
22 // Abstract factory #2
23  
24 AbstractFactory factory2 = new ConcreteFactory2();
25
26 Client client2 = new Client(factory2);
27
28 client2.Run();
29
30
31
32 // Wait for user input
33  
34 Console.ReadKey();
35
36 }
37
38 }
39
40
41
42 /// <summary>
43
44 /// The 'AbstractFactory' abstract class
45
46 /// </summary>
47
48 abstract class AbstractFactory
49 {
50
51 public abstract AbstractProductA CreateProductA();
52
53 public abstract AbstractProductB CreateProductB();
54
55 }
56
57
58
59
60
61 /// <summary>
62
63 /// The 'ConcreteFactory1' class
64
65 /// </summary>
66
67 class ConcreteFactory1 : AbstractFactory
68 {
69
70 public override AbstractProductA CreateProductA()
71 {
72
73 return new ProductA1();
74
75 }
76
77 public override AbstractProductB CreateProductB()
78 {
79
80 return new ProductB1();
81
82 }
83
84 }
85
86
87
88 /// <summary>
89
90 /// The 'ConcreteFactory2' class
91
92 /// </summary>
93
94 class ConcreteFactory2 : AbstractFactory
95 {
96
97 public override AbstractProductA CreateProductA()
98 {
99
100 return new ProductA2();
101
102 }
103
104 public override AbstractProductB CreateProductB()
105 {
106
107 return new ProductB2();
108
109 }
110
111 }
112
113
114
115 /// <summary>
116
117 /// The 'AbstractProductA' abstract class
118
119 /// </summary>
120
121 abstract class AbstractProductA
122 {
123
124 }
125
126
127
128 /// <summary>
129
130 /// The 'AbstractProductB' abstract class
131
132 /// </summary>
133
134 abstract class AbstractProductB
135 {
136
137 public abstract void Interact(AbstractProductA a);
138
139 }
140
141
142
143
144
145 /// <summary>
146
147 /// The 'ProductA1' class
148
149 /// </summary>
150
151 class ProductA1 : AbstractProductA
152 {
153
154 }
155
156
157
158 /// <summary>
159
160 /// The 'ProductB1' class
161
162 /// </summary>
163
164 class ProductB1 : AbstractProductB
165 {
166
167 public override void Interact(AbstractProductA a)
168 {
169
170 Console.WriteLine(this.GetType().Name +
171
172 " interacts with " + a.GetType().Name);
173
174 }
175
176 }
177
178
179
180 /// <summary>
181
182 /// The 'ProductA2' class
183
184 /// </summary>
185
186 class ProductA2 : AbstractProductA
187 {
188
189 }
190
191
192
193 /// <summary>
194
195 /// The 'ProductB2' class
196
197 /// </summary>
198
199 class ProductB2 : AbstractProductB
200 {
201
202 public override void Interact(AbstractProductA a)
203 {
204
205 Console.WriteLine(this.GetType().Name +
206
207 " interacts with " + a.GetType().Name);
208
209 }
210
211 }
212
213
214
215 /// <summary>
216
217 /// The 'Client' class. Interaction environment for the products.
218
219 /// </summary>
220
221 class Client
222 {
223
224 private AbstractProductA _abstractProductA;
225
226 private AbstractProductB _abstractProductB;
227
228
229
230 // Constructor
231
232 public Client(AbstractFactory factory)
233 {
234
235 _abstractProductB = factory.CreateProductB();
236
237 _abstractProductA = factory.CreateProductA();
238
239 }
240
241
242
243 public void Run()
244 {
245
246 _abstractProductB.Interact(_abstractProductA);
247
248 }
249
250
251
252
253 }

 

结果:

 

下面代码是一款计算机游戏使用不同的工厂创建不同的动物世界。虽然由Continent工厂创建的动物不同,但是动物之间的关联仍然是相同的。

 

1 class MainApp
2 {
3
4 /// <summary>
5
6 /// Entry point into console application.
7
8 /// </summary>
9
10 public static void Main()
11 {
12
13 // Create and run the African animal world
14
15 ContinentFactory africa = new AfricaFactory();
16
17 AnimalWorld world = new AnimalWorld(africa);
18
19 world.RunFoodChain();
20
21
22
23 // Create and run the American animal world
24
25 ContinentFactory america = new AmericaFactory();
26
27 world = new AnimalWorld(america);
28
29 world.RunFoodChain();
30
31
32
33 // Wait for user input
34
35 Console.ReadKey();
36
37 }
38
39 }
40
41
42
43
44
45 /// <summary>
46
47 /// The 'AbstractFactory' abstract class
48
49 /// </summary>
50
51 abstract class ContinentFactory
52 {
53
54 public abstract Herbivore CreateHerbivore();
55
56 public abstract Carnivore CreateCarnivore();
57
58 }
59
60
61
62 /// <summary>
63
64 /// The 'ConcreteFactory1' class
65
66 /// </summary>
67
68 class AfricaFactory : ContinentFactory
69 {
70
71 public override Herbivore CreateHerbivore()
72 {
73
74 return new Wildebeest();
75
76 }
77
78 public override Carnivore CreateCarnivore()
79 {
80
81 return new Lion();
82
83 }
84
85 }
86
87
88
89 /// <summary>
90
91 /// The 'ConcreteFactory2' class
92
93 /// </summary>
94
95 class AmericaFactory : ContinentFactory
96 {
97
98 public override Herbivore CreateHerbivore()
99 {
100
101 return new Bison();
102
103 }
104
105 public override Carnivore CreateCarnivore()
106 {
107
108 return new Wolf();
109
110 }
111
112 }
113
114
115
116 /// <summary>
117
118 /// The 'AbstractProductA' abstract class
119
120 /// </summary>
121
122 abstract class Herbivore
123 {
124
125 }
126
127
128
129 /// <summary>
130
131 /// The 'AbstractProductB' abstract class
132
133 /// </summary>
134
135 abstract class Carnivore
136 {
137
138 public abstract void Eat(Herbivore h);
139
140 }
141
142
143
144 /// <summary>
145
146 /// The 'ProductA1' class
147
148 /// </summary>
149
150 class Wildebeest : Herbivore
151 {
152
153 }
154
155
156
157 /// <summary>
158
159 /// The 'ProductB1' class
160
161 /// </summary>
162
163 class Lion : Carnivore
164 {
165
166 public override void Eat(Herbivore h)
167 {
168
169 // Eat Wildebeest
170
171 Console.WriteLine(this.GetType().Name +
172
173 " eats " + h.GetType().Name);
174
175 }
176
177 }
178
179
180
181 /// <summary>
182
183 /// The 'ProductA2' class
184
185 /// </summary>
186
187 class Bison : Herbivore
188 {
189
190 }
191
192
193
194 /// <summary>
195
196 /// The 'ProductB2' class
197
198 /// </summary>
199
200 class Wolf : Carnivore
201 {
202
203 public override void Eat(Herbivore h)
204 {
205
206 // Eat Bison
207
208 Console.WriteLine(this.GetType().Name +
209
210 " eats " + h.GetType().Name);
211
212 }
213
214 }
215
216
217
218 /// <summary>
219
220 /// The 'Client' class
221
222 /// </summary>
223
224 class AnimalWorld
225 {
226
227 private Herbivore _herbivore;
228
229 private Carnivore _carnivore;
230
231
232
233 // Constructor
234
235 public AnimalWorld(ContinentFactory factory)
236 {
237
238 _carnivore = factory.CreateCarnivore();
239
240 _herbivore = factory.CreateHerbivore();
241
242 }
243
244
245
246 public void RunFoodChain()
247 {
248
249 _carnivore.Eat(_herbivore);
250
251 }
252
253 }

 

结果:

总结:

posted @ 2010-11-29 13:45  麒麟  阅读(1032)  评论(2编辑  收藏  举报