设计模式--结构型--适配器模式
2012-11-22 13:58
260 查看
一、适配器模式简介(Brief Introduction)
适配器模式,将一个类装换成客户期望的另外一个接口。Adapter模式使的原本由于接口不兼容而不能工作的那些类可以一起工作。二、解决的问题(What To Solve)
1、使用第三方组件,而这个组件的接口与目前系统接口不兼容(如方法与系统方法不一致等),可以使用适配器模式解决接口不兼容问题。2、使用早前项目一些有用的类,可以用适配器模式解决现有接口与原有对象接口不兼容问题。
三、适配器模式分析(Analysis)
Client:客户端,调用自己需要的领域接口Target
Target:定义客户端需要的跟特定领域相关的接口
Adaptee:已经存在的接口,通常能满足客户端的功能要求,但是接口与客户端要求的特定领域接口不一致,需要被适配
Adapter:适配器,把Adaptee适配成为Client需要的Target
四、实例代码
1、Adaptee代码public class Adaptee { public void specificRequest(){ System.out.println("已存在接口方法调用......"); } }
2、Target接口及其实现
public interface Target { public void request(); }
public class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee){ this.adaptee = adaptee; } public void request() { System.out.println("======适配器调用已存在接口方法前======"); adaptee.specificRequest(); System.out.println("======适配器调用已存在接口方法后======"); } }
3、客户端调用
public class Client { public static void main(String[] args) { Proxy proxy = new Proxy(); proxy.request(); } }
运行结果:
相关文章推荐
- Java设计模式(7)——结构型模式之适配器模式(Adapter)
- Java结构型设计模式——适配器模式、代理模式
- 设计模式(结构型)之适配器模式(Adapter Pattern)
- 设计模式(六):适配器模式Adapter(结构型模式)
- "围观"设计模式(11)--结构型之适配器模式(Adapter Pattern)
- 设计模式(6)-结构型-适配器模式(Adapter)(个人笔记)
- 23种设计模式--结构型模式(适配器模式、装饰模式、代理模式、外观模式、桥接模式、组合模式、享元模式)
- GoF23种设计模式之结构型模式之适配器模式
- 白话设计模式--结构型模式之 适配器模式(Adapter)
- 设计模式06: Adapter 适配器模式(结构型模式)
- Java经典设计模式-结构型模式-适配器模式(Adapter)
- C#设计模式之六适配器模式(Adapter Pattern)【结构型】
- 设计模式(五)适配器模式Adapter(结构型)
- 设计模式(8)の结构型の适配器模式(Adapter)
- 设计模式(六)适配器模式(Adapter)-结构型
- GoF23种设计模式之结构型模式之适配器模式
- java结构型设计模式——适配器模式
- 设计模式(五)适配器模式(结构型)
- 结构型设计模式-适配器模式
- 设计模式之结构型模式—— 2.1 适配器模式