适配器模式
来源:互联网 发布:ipad老是无法加入网络 编辑:程序博客网 时间:2024/06/14 20:28
什么是适配器模式
顾名思义,适配器模式(Adapter Pattern)当然是用来适配的啦。当你想使用一个已有的类,但是这个类的接口跟你的又不一样,不能拿来直接用,这个时候你就需要一个适配器来帮你了。
这就好像你兴冲冲地跑去香港,买了个港版的 iPhone6,充电器插头拿回家一看,不能用啊。这时候你多么需要买一个转接头适配器...
适配器模式类图
你去香港旅游,买的 iPhone6 的充电器插头是英标的,它是那种三脚是方形的插头。
而咱们国标的插头是两只脚,即使是三只脚的插头也和英标不一样。适配器模式示例代码
在国内的家中只能用国标接口进行充电。
// 国标插头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; } // 这是重点,适配器实现了英标的插头,然后重载国标的充电方法为英标的方法 @Overridepublic 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 进行充电。
适配器模式的应用
前面已经说了,当你想使用一个已有的类,但是这个类的接口跟你的又不一样,不能拿来直接用,这个时候你就需要一个适配器来帮你了,其主要作用就是在旧的接口、新的接口之间完成适配。
比如说,做过 Android 的同学肯定写 ListView 的适配器都写吐了吧...
适配器模式的三个特点:
- 适配器对象实现原有接口
- 适配器对象组合一个实现新接口的对象(这个对象也可以不实现一个接口,只是一个单纯的对象)
- 对适配器原有接口方法的调用被委托给新接口的实例的特定方法
阅读全文
0 0
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- TF-IDF与余弦相似性的应用(一):自动提取关键词
- HEVC 帧内预测xPredIntraAng
- 深度学习总结(五)——各优化算法
- Ubuntu系统安装Ruby
- 第14周 项目1-1
- 适配器模式
- jqWEUI的select组件加载后端数据为items
- Java基础——从键盘(控制台)输入字符串(数据)的几种方式详解
- eclipse tomcat add and remove工程异常
- 主题模型(LDA)(二)-公式推导
- python dota2数据 3 下载胜负数据
- poj1163
- AC自动机
- upstream timed out (110: Conn ection timed out)