适配器模式
来源:互联网 发布:艾美依航空制造 知乎 编辑:程序博客网 时间:2024/06/09 22:41
适配器模式(Adapter Pattern)适用于两个不兼容的接口做兼容处理,属于结构性设计模式。这种模式涉及到一个单一的类,该类负责整合不兼容接口的处理。
适配器模式在现在生活中有许多例子,比如笔记本电源适配器,是作为笔记本电源和交流电源之间的适配。USB接口是整合其它设备与笔记本的适配等等。
public interface IPower { int output();}
public class Power implements IPower{ @Override public int output() { return 220; }}
public class PcPowerAdapter implements IPower{ IPower power; public PcPowerAdapter(Power power) { this.power = power; } @Override public int output() { return power.output() - 208;//220 -208 = 12 }}
public class PC { private PcPowerAdapter adapter; public PC(PcPowerAdapter adapter) { this.adapter = adapter; } public void use() { System.out.println("使用" + adapter.output() + "V电源"); }}
public class AdapterPattern { public static void main(String[] args) { Power power = new Power(); PcPowerAdapter adapter = new PcPowerAdapter(power); PC pc = new PC(adapter); pc.use(); }}
运行结果
阅读全文
0 0
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 最大公约数和最小公倍数
- Banner使用
- iOS- 非整星的评分控件(支持小数)
- prototype与__prototype__与原型链
- 鸟哥的Linux私房菜——基础学习篇(第三版)(8-12章)
- 适配器模式
- zookeeper+kafka+storm+flume+log4j
- 跟我学Dubbo系列之Java SPI机制简介
- 如何用命令将本地项目上传到github
- 有关utf-8的介绍
- FastJson常见问题
- Maven
- C# 获取当前星期 中文或者数字
- Android Studio 3.0 Jni开发环境配置 cmake编译 多个C/C++文件添加配置