设计模式之适配器模式
来源:互联网 发布:串口调试软件 编辑:程序博客网 时间:2024/06/08 01:33
适配器模式
1 什么是适配器模式
顾名思义,适配器模式(Adapter Pattern)当然是用来适配的啦。当你想使用一个已有的类,但是这个类的接口跟你的又不一样,不能拿来直接用,这个时候你就需要一个适配器来帮你了。
这就好像你兴冲冲地跑去香港,买了个港版的 iPhone6,充电器插头拿回家一看,不能用啊。这时候你多么需要买一个转接头适配器…
2 适配器模式类图
你去香港旅游,买的 iPhone6 的充电器插头是英标的,它是那种三脚是方形的插头。
而咱们国标的插头是两只脚,即使是三只脚的插头也和英标不一样。
为了方便,这里我们就假设国标插头就只是两只脚的插头吧。
好的,目标明确,英标三只脚插头充电,国标两只脚插头充电。你家很富,有很多插座可以充电。
3 适配器模式示例代码
在国内的家中只能用国标接口进行充电。
// 国标插头public interface CnPluginInterface { void chargeWith2Pins();}// 实现国标插座的充电方法public class CnPlugin implements CnPluginInterface { public void chargeWith2Pins() { System.out.println("charge with CnPlugin"); }}// 在国家中内充电public class Home { private CnPluginInterface cnPlugin; public Home() { } public Home(CnPluginInterface cnPlugin) { this.cnPlugin = cnPlugin; } public void setPlugin(CnPluginInterface cnPlugin) { this.cnPlugin = cnPlugin; } // 充电 public void charge() { // 国标充电 cnPlugin.chargeWith2Pins(); }}// 国标测试类public class CnTest { public static void main(String[] args) { CnPluginInterface cnPlugin = new CnPlugin(); Home home = new Home(cnPlugin); // 会输出 “charge with CnPlugin” home.charge(); }}
然而,当把 iPhone6 带回来时,因为与家里的插座不匹配,所以需要一个适配器。这个适配器必须满足以下条件:
必须符合国内标准的接口,否则的话还是没办法插到国内插座中;
在调用上面实现的国标接口进行充电时,提供一种机制,将这个调用转到对英标接口的调用 。
这就要求:
适配器必须实现原有的旧的接口
适配器对象中持有对新接口的引用,当调用旧接口时,将这个调用委托给实现新接口的对象来处理,也就是在适配器对象中组合一个新接口。
// 英标插头public interface EnPluginInterface { void chargeWith3Pins();}// 实现英标插座的充电方法public class EnPlugin implements EnPluginInterface { public void chargeWith3Pins() { System.out.println("charge with EnPlugin"); }}//适配器public class PluginAdapter implements CnPluginInterface { private EnPluginInterface enPlugin; public PluginAdapter(EnPluginInterface enPlugin) { this.enPlugin = enPlugin; } // 这是重点,适配器实现了英标的插头,然后重载国标的充电方法为英标的方法 @Override public void chargeWith2Pins() { enPlugin.chargeWith3Pins(); }}// 适配器测试类public class AdapterTest { public static void main(String[] args) { EnPluginInterface enPlugin = new EnPlugin(); Home home = new Home(); PluginAdapter pluginAdapter = new PluginAdapter(enPlugin); home.setPlugin(pluginAdapter); // 会输出 “charge with EnPlugin” home.charge(); }}
这样就实现了对 iphone6 进行充电。
4 适配器模式的应用
前面已经说了,当你想使用一个已有的类,但是这个类的接口跟你的又不一样,不能拿来直接用,这个时候你就需要一个适配器来帮你了,其主要作用就是在旧的接口、新的接口之间完成适配。
比如说,做过 Android 的同学肯定写 ListView 的适配器都写吐了吧…
总结
适配器模式的三个特点:
1.适配器对象实现原有接口
2.适配器对象组合一个实现新接口的对象(这个对象也可以不实现一个接口,只是一个单纯的对象)
3.对适配器原有接口方法的调用被委托给新接口的实例的特定方法
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器
- 设计模式 之 适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式之适配器
- 设计模式 之 适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之适配器模式
- 设计模式之--适配器模式.
- Linux字符设备驱动之LCD驱动
- text-overflow文本溢出的全兼容CSS代码
- Python 中文编码
- 26 管理事务处理
- kAFL:OS内核的硬件辅助反馈模糊
- 设计模式之适配器模式
- js 简单工厂模式
- 图像分割crop设置 caffe fully convolutional cnn
- ionic2开发(五)返回按钮处理
- Angular 文件上传自定义
- E: Unable to locate package oracle-java7-installer [Ubuntu 13]
- apache-ab并发负载压力测试
- 安卓开发:模仿微信,QQ评论输入框,使用PopupWindow完美实现(新版)
- js 安全的工厂模式