Java设计模式——适配器设计模式
2016-01-16 13:17
597 查看
1.定义
把一个类的接口转换成客户希望的另一种接口
适配器模式使原本不兼容而不能一起工作的类可以一起工作
2.代码示例
把一个类的接口转换成客户希望的另一种接口
适配器模式使原本不兼容而不能一起工作的类可以一起工作
2.代码示例
public class Adapter { public static void main(String[] args) { PowerA a = new PowerAImpl(); //a.connect(); intput(a); PowerB b = new PowerBImpl(); //intput(b);//不能这么用,input方法只能接收PowerA的接口 PowerAdapter powerAdapter = new PowerAdapter(b); intput(powerAdapter); } public static void intput(PowerA a) { a.connect(); } } //适配器 class PowerAdapter implements PowerA { private PowerB b; public PowerAdapter(PowerB b) { this.b = b; } public void connect() { b.insert(); } } interface PowerA { public void connect(); } class PowerAImpl implements PowerA { public void connect() { System.out.println("电源A接口开始工作"); } } interface PowerB { public void insert(); } class PowerBImpl implements PowerB { public void insert() { System.out.println("电源B接口开始工作"); } }
相关文章推荐
- Java线程未捕获异常处理
- 深入理解java异常处理机制
- struts2 使用ajax 传递json数据
- cmd运行java 找不到或无法加载主类的问题
- Java实现通过smtp服务器验证邮箱的真实有效性
- Struts2_ValueStack,OGNL详解
- 2、Java基础
- Spring下载方法
- JAVA文件监听
- 1、Java综述
- Struts2中的ModelDriven机制及其运用
- Java实现将选择题题库按照字母顺序重新排列
- SpringMVC返回文件流
- java中动态代理实现机制
- Struts2源码浅析-ConfigurationProvider
- Ubuntu14.04 JAVA环境配置
- Java Collection
- Java设计模式——静态代理设计模式
- ubuntu安装jdk全过程-转
- Java高级特性之枚举