《Java与模式》学习笔记之四——适配器(Adapter)模式
来源:互联网 发布:汽车租车软件 编辑:程序博客网 时间:2024/04/27 21:40
适配器模式(Adapter Pattern)[GOF95]把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
问题:
我们已经在应用程序中设计了接口sampleOperation1, 然后我们又为了加快开发而购买了可以很好的完成此功能的第三方库, 然而第三方库的接口sampleOperation2和我们设计的接口sampleOperation1不同, 为了使这些接口不兼容的类可以一起工作, 我们应用Adapter模式将第三方库的接口sampleOperation2转换为我们的接口sampleOperation1 (也是客户希望的接口).
适配器模式有两种形式.
一、 类的适配器模式
源代码:
package com.designpatterns.adapter;
/**
* 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口
* @author suki
*/
public interface Target {
public void sampleOperation1();
}
/**
* 源角色,相当于第三方库
* @author suki
*/
public class Adaptee {
public Adaptee() {
}
public void sampleOperation2()
{
System.out.println("Adaptee.sampleOperation2()");
}
}
/**
* 适配器角色
* 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1
* * @author suki
*/
public class Adapter extends Adaptee implements Target {
public void sampleOperation1() {
this.sampleOperation2();
}
}
/**
* 客户端类
* @author suki
*/
public class Client {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter();
target.sampleOperation1();
}
}
源代码:
package com.designpatterns.adapter;
/**
* 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口
* @author suki
*/
public interface Target {
public void sampleOperation1();
}
/**
* 源角色,相当于第三方库
* @author suki
*/
public class Adaptee {
public Adaptee() {
}
public void sampleOperation2()
{
System.out.println("Adaptee.sampleOperation2()");
}
}
/**
* 适配器角色
* 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1
* * @author suki
*/
public class Adapter extends Adaptee implements Target {
public void sampleOperation1() {
this.sampleOperation2();
}
}
/**
* 客户端类
* @author suki
*/
public class Client {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter();
target.sampleOperation1();
}
}
二、 对象的适配器模式
在类的适配器模式中Adapter采用继承的方式复用Adaptee的接口,而在对象的适配器模式中Adapter则采用组合的方式实现Adaptee的复用
Interface Target +sampleOperation1:void Adaptee +sampleOperation2:void Adapter -adaptee:Adaptee +Adapter +sampleOperation1:void
源代码:
package com.designpatterns.adapterofobject;
/**
* 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口
* @author suki
*/
public interface Target {
public void sampleOperation1();
}
/**
* 源角色,相当于第三方库
* @author suki
*/
public class Adaptee {
public Adaptee() {
}
public void sampleOperation2() {
System.out.println("Adaptee.sampleOperation2()");
}
}
/**
* 适配器角色
* 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1
* @author suki
*/
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee)
{
super();
this.adaptee = adaptee;
}
public void sampleOperation1() {
adaptee.sampleOperation2();
}
}
/**
* 客户端类
* @author suki
*/
public class Client {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new Adapter(adaptee);
target.sampleOperation1();
}
}
- 《Java与模式》学习笔记之四——适配器(Adapter)模式
- 《Java与模式》学习笔记之四——适配器(Adapter)模式
- 《Java与模式》学习笔记:设计模式——适配器(Adapter)模式
- java设计模式之— 适配器(Adapter)
- 设计模式学习笔记---适配器模式adapter(Java版)
- 设计模式学习笔记(七)——Adapter适配器
- 设计模式学习笔记——适配器(Adapter)模式
- 设计模式学习笔记(二)—-Adapter适配器模式
- 【四】设计模式——适配器模式(Adapter Pattern)
- 设计模式学习笔记(七)之适配器模式(Adapter)
- 适配器模式-Adapter Pattern 不兼容结构的协调——适配器模式(四):缺省适配器,适配器模式总结
- java 设计模式学习笔记五Adapter适配器模式
- Adapter—适配器模式
- java设计模式——适配器模式(Adapter)
- java设计模式6——适配器模式(Adapter)
- Java设计模式——适配器模式(Adapter Pattern)
- Java设计模式——适配器模式(Adapter Pattern)
- 《java与模式》读书笔记之九----适配器模式(Adapter)
- 用.net 2003开发SmartPhone调试出现无法建立连接问题
- XSL-FO 高级技术7
- Eclipse快速上手指南之使用CVS
- XSL-FO 高级技术8
- Eclipse快速上手指南之使用JUnit
- 《Java与模式》学习笔记之四——适配器(Adapter)模式
- Eclipse快速上手指南之使用ANT
- 今天是我的生日,为自己祝贺一下,呵呵
- XSL-FO 高级技术9
- 排序
- AJAX基础教程
- ORACLE中UTL_FILE ,DBMS-SQL进行数据库与文件互操作的存储过程
- WEB标准
- Windows Mobile开发环境搭建指南