http://xiangai.taobao.com
http://shop148612228.taobao.com

设计模式学习九:适配器模式

设计模式学习九:适配器模式

设计模式

一.概念

     适配器模式将一个接口转换成客户希望的另外一个接口。它使得原来由于接口不兼容而不能在一起工作的那些类可以一起工作。

二.UML

三.更加生动的例子

四.实例分析

     去年买了一个本本,另外给本本配了罗技G1光电套。坑爹的是,光电套的鼠标是USB接口,键盘是PS2接口,可我的本本却没有PS2接口啊。于是跑到市场,淘了一个转接器。

      于是乎,我抽象了这么几个类。

  1. PS2Port(PS2接口)。
  2. USBPort(USB接口)。
  3. PS2ToUSB(对象适配器),将PS2接口装换成USB接口。
  4. TestAdapter(测试类),客户端。

     PS2Port

Java代码 收藏代码

  1. package com.zzy.adapter; 
  2. /**
  3. * PS2接口,圆口
  4. * @author eason
  5. *
  6. */
  7. public interface PS2Port { 
  8. public void workWithPS2(); 

     USBPort

Java代码 收藏代码

  1. package com.zzy.adapter; 
  2. /**
  3. * USB接口,U口
  4. * @author eason
  5. *
  6. */
  7. public interface USBPort { 
  8. public void workWithUSB(); 

     PS2ToUSB

Java代码 收藏代码

  1. package com.zzy.adapter; 
  2. /**
  3. * 对象适配器
  4. * 将PS2接口装换成USB接口
  5. * 所以此类类型是USB接口(implements USBPort) + 成员变量ps2Port
  6. * @author eason
  7. *
  8. */
  9. public class PS2ToUSB implements USBPort{ 
  10. private PS2Port ps2Port; 
  11. public PS2ToUSB(PS2Port ps2Port) { 
  12. this.ps2Port = ps2Port; 
  13.     } 
  14. @Override
  15. public void workWithUSB() { 
  16.         System.out.println("转换的关键在这里,本来是"); 
  17.         ps2Port.workWithPS2(); 
  18.         System.out.println("经过你的转换,现在是USB工作中"); 
  19.     } 

     TestAdapter

Java代码 收藏代码

  1. package com.zzy.adapter; 
  2. /**
  3. * 测试类
  4. * client
  5. * @author eason
  6. *
  7. */
  8. public class TestAdapter { 
  9. public static void main(String[] args) { 
  10. //1.我现在有一个PS2接口
  11.         PS2Port ps2Port = new PS2Port() { 
  12. @Override
  13. public void workWithPS2() { 
  14.                 System.out.println("PS2工作中"); 
  15.             } 
  16.         }; 
  17. //2.但是我需要的是一个USB接口啊,对我(client)来说,我只认识USB接口
  18. //3.经过PS2ToUSB的转换,PS2接口变成了USB接口
  19.         USBPort ps2ToUsbPort = new PS2ToUSB(ps2Port); 
  20.         ps2ToUsbPort.workWithUSB(); 
  21.     } 

五.对象的适配器和类的适配器

     上述的适配器就是对象适配器。再看看类适配器。

     PS2ToUSB,只是简单模拟一下。因为java不允许多继承,所以java中没有类模式的代码,只有思想。

Java代码 收藏代码

  1. package com.zzy.adapter; 
  2. /**
  3. * 类适配器
  4. * @author eason
  5. *
  6. */
  7. public class PS2ToUSB implements USBPort, PS2Port{ 
  8. //重写workWithUSB,把工作交给workWithPS2
  9. @Override
  10. public void workWithUSB() { 
  11.         workWithPS2(); 
  12.     } 

      差别就是:对象适配器实现了客户端想要的接口(USB),在内部有一个被适配对象(PS2)的引用,通过组合实现适配功能。类适配器实现了客户端想要的接口(USB)和被适配对象接口(PS2),通过继承来实现适配功能。

六.使用场景及使用感受

  1. 希望复用一些现存的类,但是接口又与复用环境要求不一致。
  2. 其实适配器模式有点无奈之举,在前期设计的时候,我们就不应该考虑适配器模式,而应该考虑通过重构统一接口。

七.适配器模式与装饰者模式

     它们都可以用来包装对象,本质区别在于

  1. 适配器模式:将一个接口转换成另外一个接口。
  2. 装饰者模式:不改变接口,只加入职责。
posted @ 2013-11-28 20:00  万事俱备就差个程序员  阅读(153)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏