定义:将一个类的接口转换为另外一个用户所期望的接口。
Adapter设计模式允许一些类可以共同运行,这些类因为具有不兼容的接口而不能使用其它的方法。
UML:

示例:
View Code
1 // Adapter pattern -- Structural example
2
3
4 using System;
5
6
7 namespace DoFactory.GangOfFour.Adapter.Structural
8
9 {
10
11 /// <summary>
12
13 /// MainApp startup class for Structural
14
15 /// Adapter Design Pattern.
16
17 /// </summary>
18
19 class MainApp
20
21 {
22
23 /// <summary>
24
25 /// Entry point into console application.
26
27 /// </summary>
28
29 static void Main()
30
31 {
32
33 // Create adapter and place a request
34
35 Target target = new Adapter();
36
37 target.Request();
38
39
40
41 // Wait for user
42
43 Console.ReadKey();
44
45 }
46
47 }
48
49
50
51 /// <summary>
52
53 /// The 'Target' class
54
55 /// </summary>
56
57 class Target
58
59 {
60
61 public virtual void Request()
62
63 {
64
65 Console.WriteLine("Called Target Request()");
66
67 }
68
69 }
70
71
72
73 /// <summary>
74
75 /// The 'Adapter' class
76
77 /// </summary>
78
79 class Adapter : Target
80
81 {
82
83 private Adaptee _adaptee = new Adaptee();
84
85
86
87 public override void Request()
88
89 {
90
91 // Possibly do some other work
92
93 // and then call SpecificRequest
94
95 _adaptee.SpecificRequest();
96
97 }
98
99 }
100
101
102
103 /// <summary>
104
105 /// The 'Adaptee' class
106
107 /// </summary>
108
109 class Adaptee
110
111 {
112
113 public void SpecificRequest()
114
115 {
116
117 Console.WriteLine("Called SpecificRequest()");
118
119 }
120
121 }
122
123 }
124
输出:
Called SpecificRequest()
View Code
1 // Adapter pattern -- Real World example
2
3
4 using System;
5
6
7 namespace DoFactory.GangOfFour.Adapter.RealWorld
8
9 {
10
11 /// <summary>
12
13 /// MainApp startup class for Real-World
14
15 /// Adapter Design Pattern.
16
17 /// </summary>
18
19 class MainApp
20
21 {
22
23 /// <summary>
24
25 /// Entry point into console application.
26
27 /// </summary>
28
29 static void Main()
30
31 {
32
33 // Non-adapted chemical compound
34
35 Compound unknown = new Compound("Unknown");
36
37 unknown.Display();
38
39
40
41 // Adapted chemical compounds
42
43 Compound water = new RichCompound("Water");
44
45 water.Display();
46
47
48
49 Compound benzene = new RichCompound("Benzene");
50
51 benzene.Display();
52
53
54
55 Compound ethanol = new RichCompound("Ethanol");
56
57 ethanol.Display();
58
59
60
61 // Wait for user
62
63 Console.ReadKey();
64
65 }
66
67 }
68
69
70
71 /// <summary>
72
73 /// The 'Target' class
74
75 /// </summary>
76
77 class Compound
78
79 {
80
81 protected string _chemical;
82
83 protected float _boilingPoint;
84
85 protected float _meltingPoint;
86
87 protected double _molecularWeight;
88
89 protected string _molecularFormula;
90
91
92
93 // Constructor
94
95 public Compound(string chemical)
96
97 {
98
99 this._chemical = chemical;
100
101 }
102
103
104
105 public virtual void Display()
106
107 {
108
109 Console.WriteLine("\nCompound: {0} ------ ", _chemical);
110
111 }
112
113 }
114
115
116
117 /// <summary>
118
119 /// The 'Adapter' class
120
121 /// </summary>
122
123 class RichCompound : Compound
124
125 {
126
127 private ChemicalDatabank _bank;
128
129
130
131 // Constructor
132
133 public RichCompound(string name)
134
135 : base(name)
136
137 {
138
139 }
140
141
142
143 public override void Display()
144
145 {
146
147 // The Adaptee
148
149 _bank = new ChemicalDatabank();
150
151
152
153 _boilingPoint = _bank.GetCriticalPoint(_chemical, "B");
154
155 _meltingPoint = _bank.GetCriticalPoint(_chemical, "M");
156
157 _molecularWeight = _bank.GetMolecularWeight(_chemical);
158
159 _molecularFormula = _bank.GetMolecularStructure(_chemical);
160
161
162
163 base.Display();
164
165 Console.WriteLine(" Formula: {0}", _molecularFormula);
166
167 Console.WriteLine(" Weight : {0}", _molecularWeight);
168
169 Console.WriteLine(" Melting Pt: {0}", _meltingPoint);
170
171 Console.WriteLine(" Boiling Pt: {0}", _boilingPoint);
172
173 }
174
175 }
176
177
178
179 /// <summary>
180
181 /// The 'Adaptee' class
182
183 /// </summary>
184
185 class ChemicalDatabank
186
187 {
188
189 // The databank 'legacy API'
190
191 public float GetCriticalPoint(string compound, string point)
192
193 {
194
195 // Melting Point
196
197 if (point == "M")
198
199 {
200
201 switch (compound.ToLower())
202
203 {
204
205 case "water": return 0.0f;
206
207 case "benzene": return 5.5f;
208
209 case "ethanol": return -114.1f;
210
211 default: return 0f;
212
213 }
214
215 }
216
217 // Boiling Point
218
219 else
220
221 {
222
223 switch (compound.ToLower())
224
225 {
226
227 case "water": return 100.0f;
228
229 case "benzene": return 80.1f;
230
231 case "ethanol": return 78.3f;
232
233 default: return 0f;
234
235 }
236
237 }
238
239 }
240
241
242
243 public string GetMolecularStructure(string compound)
244
245 {
246
247 switch (compound.ToLower())
248
249 {
250
251 case "water": return "H20";
252
253 case "benzene": return "C6H6";
254
255 case "ethanol": return "C2H5OH";
256
257 default: return "";
258
259 }
260
261 }
262
263
264
265 public double GetMolecularWeight(string compound)
266
267 {
268
269 switch (compound.ToLower())
270
271 {
272
273 case "water": return 18.015;
274
275 case "benzene": return 78.1134;
276
277 case "ethanol": return 46.0688;
278
279 default: return 0d;
280
281 }
282
283 }
284
285 }
286
287 }
288
输出:
Compound: Unknown ------
Compound: Water ------ Formula: H20 Weight : 18.015 Melting Pt: 0 Boiling Pt: 100
Compound: Benzene ------ Formula: C6H6 Weight : 78.1134 Melting Pt: 5.5 Boiling Pt: 80.1
Compound: Alcohol ------ Formula: C2H6O2 Weight : 46.0688 Melting Pt: -114.1 Boiling Pt: 78.3

浙公网安备 33010602011771号