经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式-结构型-适配器模式
来源:cnblogs  作者:酷学大叔  时间:2019/9/19 10:02:24  对本文有异议

适配器模式(Adapter Pattern):

  将某个类的接口转换成客户端期望的另一个接口表示,主要的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。如读卡器是作为内存卡和笔记本之间的适配器,需要将内存卡插入读卡器,再将读卡器插入笔记本,这样笔记本就可以读取内存卡了。

适配器模式的主要角色:

  

  1、目标接口(Target):当前系统业务所期待的接口,它可以是抽象类或接口。

  2、适配者类(Adaptee):被访问和适配的现存组件库中的组件接口。

  3、适配器类(Adapter):转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。

适配器模式主要分三类:类适配器模式、对象适配器模式、接口适配器模式(java中使用)。接下来我们来详细介绍下这三种形式。

  1、类适配器模式

    当前去外地出差,工作繁忙,回到宾馆也不得不工作。当你拿出电脑,准备接电源,卧槽...不支持!!!电源是两孔的,而插座是三孔的,怎么办呢?你会想到去楼下小商店买个转换插座,将三孔插座转成两孔的来用,简直太聪明了。 

    (以该图代替,原理类似)

  1. 1 internal class Program
  2. 2 {
  3. 3 private static void Main(string[] args)
  4. 4 {
  5. 5 Computer computer = new Computer(new TwoHoleSocket());
  6. 6 computer.Connect();
  7. 7 // 类适配器的缺点
  8. 8 TwoHoleSocket twoHoleSocket = new TwoHoleSocket();
  9. 9 twoHoleSocket.Test();
  10. 10 }
  11. 11 }
  12. 12
  13. 13 internal class Computer
  14. 14 {
  15. 15 private readonly Transformer transformer;
  16. 16
  17. 17 public Computer(Transformer transformer)
  18. 18 {
  19. 19 this.transformer = transformer;
  20. 20 }
  21. 21
  22. 22 public void Connect()
  23. 23 {
  24. 24 transformer.Transfrom();
  25. 25 Console.WriteLine("连接成功!!!");
  26. 26 }
  27. 27 }
  28. 28
  29. 29 /// <summary>
  30. 30 /// 三孔插座Adaptee
  31. 31 /// </summary>
  32. 32 internal class ThreeHoleSocket
  33. 33 {
  34. 34 public void Show()
  35. 35 {
  36. 36 Console.WriteLine("我是三孔插座");
  37. 37 }
  38. 38
  39. 39 public void Test()
  40. 40 {
  41. 41 Console.WriteLine("我是多余的");
  42. 42 }
  43. 43 }
  44. 44
  45. 45 /// <summary>
  46. 46 /// 转换器Target
  47. 47 /// </summary>
  48. 48 internal interface Transformer
  49. 49 {
  50. 50 void Transfrom();
  51. 51 }
  52. 52
  53. 53 /// <summary>
  54. 54 /// 两孔插座Adapter
  55. 55 /// </summary>
  56. 56 internal class TwoHoleSocket : ThreeHoleSocket, Transformer
  57. 57 {
  58. 58 public void Transfrom()
  59. 59 {
  60. 60 base.Show();
  61. 61 Console.WriteLine("转换中。。。。");
  62. 62 Console.WriteLine("哈哈哈,我已经是两孔插座");
  63. 63 }
  64. 64 }
view code

    分析:对于类适配器来说,Adapter与Adaptee过于耦合,增加了使用成本,且会暴露不必要的方法。

  2、对象适配器模式

    当手机没电需要充电,你不可能直接使用220V的电压,如果你真这么做了,请注意人身安全及购买一台新机。此时,充电器就起到了转换器的作用,将220V电压转换成手机可使用的5V电压。

    

  1. 1 class Program
  2. 2 {
  3. 3 static void Main(string[] args)
  4. 4 {
  5. 5 Phone phone = new Phone();
  6. 6 phone.charging(new VoltageAdapter(new Voltage220V()));
  7. 7 }
  8. 8 }
  9. 9
  10. 10 class Phone
  11. 11 {
  12. 12 public void charging(Voltage5V voltage5V)
  13. 13 {
  14. 14 voltage5V.output5V();
  15. 15 }
  16. 16 }
  17. 17
  18. 18 class Voltage220V
  19. 19 {
  20. 20 public void output220V()
  21. 21 {
  22. 22 Console.WriteLine("220V");
  23. 23 }
  24. 24 }
  25. 25
  26. 26 interface Voltage5V
  27. 27 {
  28. 28 void output5V();
  29. 29 }
  30. 30
  31. 31 class VoltageAdapter : Voltage5V
  32. 32 {
  33. 33 private readonly Voltage220V voltage220V;
  34. 34
  35. 35 public VoltageAdapter(Voltage220V voltage220V)
  36. 36 {
  37. 37 this.voltage220V = voltage220V;
  38. 38 }
  39. 39
  40. 40 public void output5V()
  41. 41 {
  42. 42 voltage220V.output220V();
  43. 43 Console.WriteLine("5V");
  44. 44 }
  45. 45 }
view code

    分析:以松耦合的方式实现适配器模式,推荐使用。   

  3、接口适配器模式(java中使用,c#中能够实现但意义不大)

     

  1. 1 class Program
  2. 2 {
  3. 3 static void Main(string[] agrs)
  4. 4 {
  5. 5 Power5VAdapter power5VAdapter = new Power5VAdapter(new AC220());
  6. 6 Console.WriteLine(power5VAdapter.output5V());
  7. 7 }
  8. 8 }
  9. 9
  10. 10 class AC220
  11. 11 {
  12. 12 public int output220V()
  13. 13 {
  14. 14 int output = 220;
  15. 15 return output;
  16. 16 }
  17. 17 }
  18. 18
  19. 19 interface Voltage
  20. 20 {
  21. 21 int output5V();
  22. 22 int output9V();
  23. 23 int output12V();
  24. 24 int output24V();
  25. 25 }
  26. 26
  27. 27 abstract class PowerAdapter : Voltage
  28. 28 {
  29. 29 private readonly AC220 ac220;
  30. 30
  31. 31 public PowerAdapter(AC220 ac220)
  32. 32 {
  33. 33 this.ac220 = ac220;
  34. 34 }
  35. 35
  36. 36 public virtual int output12V()
  37. 37 {
  38. 38 return ac220.output220V();
  39. 39 }
  40. 40
  41. 41 public virtual int output24V()
  42. 42 {
  43. 43 return ac220.output220V();
  44. 44 }
  45. 45
  46. 46 public virtual int output5V()
  47. 47 {
  48. 48 return ac220.output220V();
  49. 49 }
  50. 50
  51. 51 public virtual int output9V()
  52. 52 {
  53. 53 return ac220.output220V();
  54. 54 }
  55. 55 }
  56. 56
  57. 57 class Power5VAdapter : PowerAdapter
  58. 58 {
  59. 59 private AC220 ac220;
  60. 60
  61. 61 public Power5VAdapter(AC220 ac220)
  62. 62 : base(ac220)
  63. 63 {
  64. 64 this.ac220 = ac220;
  65. 65 }
  66. 66
  67. 67 public override int output5V()
  68. 68 {
  69. 69 int output = 0;
  70. 70 if (this.ac220 != null)
  71. 71 {
  72. 72 output = this.ac220.output220V() / 44;
  73. 73 }
  74. 74 return output;
  75. 75 }
  76. 76 }
view code

优点:

  1、客户端通过适配器可以透明地调用目标接口。

  2、复用了现存的类,开发人员不需要修改原有代码而重用现有的适配者类。

  3、将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。

缺点:

  1、对类适配器来说,更换适配器的实现过程比较复杂。

  2、过多的适配器,会让系统零乱,不易整体进行把握。

原文链接:http://www.cnblogs.com/az4215/p/11538446.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号