abstract class PowerAdapter : Voltage
28 {
29 private readonly AC220 ac220;
30
31 public PowerAdapter(AC220 ac220)
32 {
33 this.ac220 = ac220;
34 }
35
36 public virtual int output12V()
37 {
38 return ac220.output220V();
39 }
40
41 public virtual int output24V()
42 {
43 return ac220.output220V();
44 }
45
46 public virtual int output5V()
47 {
48 return ac220.output220V();
49 }
50
51 public virtual int output9V()
52 {
53 return ac220.output220V();
54 }
55 }
56
57 class Power5VAdapter : PowerAdapter
58 {
59 private AC220 ac220;
60
61 public Power5VAdapter(AC220 ac220)
62 : base(ac220)
63 {
64 this.ac220 = ac220;
65 }
66
67 public override int output5V()
68 {
69 int output = 0;
70 if (this.ac220 != null)
71 {
72 output = this.ac220.output220V() / 44;
73 }
74 return output;
75 }
76 }
view code
优点:
1、客户端通过适配器可以透明地调用目标接口。
2、复用了现存的类,开发人员不需要修改原有代码而重用现有的适配者类。
3、将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
缺点:
1、对类适配器来说,更换适配器的实现过程比较复杂。
2、过多的适配器,会让系统零乱,不易整体进行把握。
|