适配器:基于现有类所提供的服务,向客户提供接口,以满足客户的期望。

《设计模式》一书中是这样给适配器模式定义的:将一个类的接口转换成客户希望的另外一个接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。由引子中给出的例子可知,这个定义描述的功能和现实中的适配器的功能是一致的。

《Java设计模式》

类适配器 
客户的开发人员定义了一个接口,期望用这个接口来完成整数的求和操作,接口定义如下:

public interface Operation{
public int add(int a,int b);
}

开发人员在了解这个接口的定义后,发现一个第三方类,里面有一个方法能实现他们期望的功能,其代码如下:

public class OtherOperation{
public int otherAdd(int a,int b){
return a + b;
}
}

以上第三方类OtherOperation的方法public int otherAdd(int a,int b)所提供的功能,完全能符合客户的期望,所以只需要想办法把OtherOperationotherAdd(int a,int b)和客户的Operation接口联系起来,让这个第三方类来为客户提供他们期望的服务就行了,这样就避免了开发人员再度去研究类似OtherOperationotherAdd(int a,int b)方法的实现(利用已有的轮子,避免重复发明),这方法之一,就是用适配器模式:

public class AdapterOperation extends OtherOperation implements Operation{
public int add(int a,int b){
return otherAdd(a,b);
}
}

以上就是适配器的实现方法之一,类适配器,在以上实现中存在着三中角色分别是: 
1:适配目标角色:Operation。 
2:适配类(原)角色:OtherOperation。 
3:适配器角色:AdapterOperation。 
其中适配器角色是适配器模式的核心。 
适配器的主要工作就是通过封装现有的功能,使他满足需要的接口。

对象适配器 
我们再来看看另一种情况: 
假如客户接口期望的功能不止一个,而是多个:

public interface Operation{
public int add(int a,int b);
public int minus(int a,int b);
public int multiplied(int a,int b);
}

而能提供这些实现的原可能不止一个:

public class OtherAdd{
public int otherAdd(int a,int b){
return a + b;
}
} public class OtherMinus{
public int minus(int a,int b){
return a - b;
}
} public class OtherMultiplied{
public int multiplied(int a,int b){
return a * b;
}
}

由于java是不能实现多继承的,所以我们不能通过构建一个适配器,让他来继承所有原以完成我们的期望,这时候怎么办呢?只能用适配器的另一种实现--对象适配器:

public class AdapterOperation implements Operation{
private OtherAdd add;
private OtherMinus minus;
private OtherMultiplied multiplied; public void setAdd(OtherAdd add){
this.add = add;
} public void setMinus(OtherMinus minus){
this.minus = minus;
} public void setMultiplied(OtherMultiplied multiplied){
this.multiplied = multiplied;
} //适配加法运算
public int add(int a,int b){
return add.otherAdd(a,b);
} //适配减法运算
public int minus(int a,int b){
return minus.minus(a,b);
} //适配乘法运算
public int multiplied(int a,int b){
return multiplied.multiplied(a,b);
}
}

上面代码很明显,适配器并不是通过继承来获取适配类(原)的功能的,而是通过适配类的对象来获取的,这就解决了java不能多继承所带来的不便了。这也是java提倡的编程思想之一,即尽量使用聚合不要使用继承。 
还有一种情况是需要使用对象适配器的。我们来看看, 
当我们的客户提供的需求并不是一个明确的接口,而是一个类,并没有定义期望的方法,如下

public class A{
public int add(int a,int b){
return a + b;
}
}

现在客户要一个新类B,要求能在保留类A功能的情况下增加一个运算减法的功能,并要求B能随时替换掉A但不能对已有系统造成影响。这样我们只能新建一个类B,并让B继承A。

public class B extends A{
b(){
super();
} public int minus(int a,int b){
//待实现的减法运算函数..
}
}

这时候,我们发现类C已经提供了实现减法的函数,

public class C{
public int minus(int a,int b){
return a - b;
}
}

为了避免重复去设计该函数,我们决定引入C类,通过适配C类来达到我们的期望,但问题是A和C都是一个具体类,我们无法让B同时继承这个两个类,而B继承A又是必须的,所以我们只能考虑把C给内聚到B内部,对象适配器又得派上用场了。

public class B extends A{

    private C c;

    B(){
super();
} public void setMinus(C c){
this.c= c;
} public int minus(int a,int b){
return c.minus(a,b);
}
}

这样,在需要A类的地方都能用B类来代替,同时又保证了新的功能的引入。

更灵活的实现--隐藏目标接口的抽象适配器

做java 桌面应用的都知道WindowListener接口,

public interface WindowListener extends EventListener{
  public void windowActivated(WindowEvent e);
  public void windowClosed(WindowEvent e);
  public void windowClosing(WindowEvent e);
  public void windowDeactivated(WindowEvent e);
  public void windowDeiconified(WindowEvent e);
  public void windowIconified(WindowEvent e);
  public void windowOpened(WindowEvent e);
}

要实现这个接口,我们就必须实现它所定义的所有方法,但是实际上,我们很少需要同时用到所有的方法,我们要的只是其中的两三个。为了不使我们实现多余的方法, 
jdk WindowListener提供了一个WindowListener的默认实现类WindowAdapter类,这是一个抽象类,

public abstract class WindowAdapter implements WindowListener{
   public void windowActivated(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowClosing(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
  public void windowOpened(WindowEvent e){}
}

WindowAdapter类对WindowListener接口的所有有方法都提供了空实现, 
有了WindowAdapter类,我们只需要去继承WindowAdapter,然后选择我们所关心的方法来实现就行了,这样就避免了直接去实现WindowListener接口。

原文出处:http://chjl2020.iteye.com/blog/262370

(转)适配器模式--Adapter Pattern的更多相关文章

  1. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...

  2. 设计模式 - 适配器模式(adapter pattern) 具体解释

    适配器模式(adapter pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 适配器模式(adapter pattern): 将一个类的接 ...

  3. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  4. 【设计模式】适配器模式 Adapter Pattern

    适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...

  5. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  6. 设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释

    适配器模式(adapter pattern) 枚举器和迭代器 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考适配器模式(adapter patter ...

  7. 设计模式系列之适配器模式(Adapter Pattern)——不兼容结构的协调

    模式概述 模式定义 模式结构图 模式伪代码 类适配器,双向适配器,缺省适配器 类适配器 双向适配器 缺省适配器 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 主要缺点 适 ...

  8. 设计模式(七): 通过转接头来观察"适配器模式"(Adapter Pattern)

    在前面一篇博客中介绍了“命令模式”(Command Pattern),今天博客的主题是“适配器模式”(Adapter Pattern).适配器模式用处还是比较多的,如果你对“适配器模式”理解呢,那么自 ...

  9. 适配器模式(Adapter Pattern)

    适配器模式概述 定义:将一个类的接口转化成客户希望的另一个接口,适配器模式让那些接口不兼容的类可以一起工作.别名(包装器[Wrapper]模式) 它属于创建型模式的成员,何为创建型模式:就是关注如何将 ...

  10. 七个结构模式之适配器模式(Adapter Pattern)

    定义: 将一个接口转换为客户需要的另外一个接口,使接口不兼容的类型可以一起工作,也被称为包装器模式(Wrapper Patern). 结构图: Target:目标抽象类,客户所需要的接口. Adapt ...

随机推荐

  1. 嵌入式 uboot引导kernel,kernel引导fs【转】

    转自:http://www.cnblogs.com/lidabo/p/5383934.html#3639633 1.uboot引导kernel: u-boot中有个bootm命令,它可以引导内存中的应 ...

  2. linux-open-source-development-tools【重点】

    https://www.pluralsight.com/blog/software-development/linux-open-source-development-tools https://ww ...

  3. 看jquery3.3.1学js类型判断的技巧

    需要预习:call , typeof, js数据类型 1. isFunction中typeof的不靠谱 源码: var isFunction = function isFunction( obj ) ...

  4. socket.io入门整理

    我自己在用socket.io开发,对官方网站上的文档,进行简单的整理,然后自己写了一个简单的聊天程序. 最最开始 先安装socket.io: 1 npm install socket.io 利用Nod ...

  5. opengl基础学习专题 (一 )编程环境搭建

    题外话: 第一次在博客园上同大家分享博文.水的的地方,错别字的地方.环境交流.批评.知道了马上改. 以前在百度空间中写技术分享博文,后来百度啥也没说就把整个空间封了.当时感觉 还是有点寒心.只想黑一下 ...

  6. MACBOOK 总是断网怎么办

    MACBOOK 连接 wifi 老是断网.焦躁不安 看图,二个方法,第一就搞定,

  7. 重写Alert

    var isIE = navigator.appName.indexOf("Internet Explorer") != -1;var oldAlert = window.aler ...

  8. Django内置信号

    阅读目录(Content) Django中内置的signal 自定义信号 1.定义信号 2.注册信号 3.触发信号 回到顶部(go to top) Django中内置的signal Django中提供 ...

  9. hdu5782

    官方题解不是很详细 首先有一个结论:若A=pa+sa B=pb+sb A.B串循环同构,则可以构造一个可行方案(pa,sb) (sa,pb)中有一个是最长匹配,这个不难用反证法证明. 对于s1,s2串 ...

  10. 通过url判断当前页,动态给导航加样式

    //通过url判断当前页,动态给导航加样式 var str =location.pathname; var index = str.lastIndexOf("\/"); str = ...