适配器模式定义:将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份。

为何使用适配器模式

我们经常碰到要将两个没有关系的类组合在一起使用,第一解决方案是:修改各自类的接口,但是如果我们没有源代码,或者,我们不愿意为了一个应用而修改各自的接口。 怎么办?

使用Adapter,在这两种接口之间创建一个混合接口(混血儿)。

如何使用适配器模式

实现Adapter方式,其实"think in Java"的"类再生"一节中已经提到,有两种方式:组合(composition)和继承(inheritance),

假设我们要打桩,有两种类:方形桩 圆形桩。

public class SquarePeg{
 public void insert(String str){
  System.out.println("SquarePeg insert():"+str);
 }
} public class RoundPeg{
 public void insertIntohole(String msg){
  System.out.println("RoundPeg insertIntoHole():"+msg);
}
}

现在有一个应用,需要既打方形桩,又打圆形桩。那么我们需要将这两个没有关系的类综合应用,假设RoundPeg我们没有源代码,或源代码我们不想修改,那么我们使用Adapter来实现这个应用:

public class PegAdapter extends SquarePeg{
 private RoundPeg roundPeg;
 public PegAdapter(RoundPeg peg)(this.roundPeg=peg;)
 public void insert(String str){ roundPeg.insertIntoHole(str);}
}

在上面代码中,RoundPeg属于Adaptee,是被适配者。PegAdapter是Adapter,将Adaptee(被适配者RoundPeg)和Target(目标SquarePeg)进行适配。实际上这是将组合方法(composition)和继承(inheritance)方法综合运用。

PegAdapter首先继承SquarePeg,然后使用new的组合生成对象方式,生成RoundPeg的对象roundPeg,再重载父类insert()方法。从这里,你也了解使用new生成对象和使用extends继承生成对象的不同,前者无需对原来的类修改,甚至无需要知道其内部结构和源代码。

如果你有些Java使用的经验,已经发现,这种模式经常使用。

进一步使用

上面的PegAdapter是继承了SquarePeg,如果我们需要两边继承,即继承SquarePeg 又继承RoundPeg,因为Java中不允许多继承,但是我们可以实现(implements)两个接口(interface):

public interface IRoundPeg{
 public void insertIntoHole(String msg);
}
public interface ISquarePeg{
 public void insert(String str);
}

下面是新的RoundPeg 和SquarePeg, 除了实现接口这一区别,和上面的没什么区别。

public class SquarePeg implements ISquarePeg{
 public void insert(String str){
  System.out.println("SquarePeg insert():"+str);
 }
} public class RoundPeg implements IRoundPeg{
 public void insertIntohole(String msg){
  System.out.println("RoundPeg insertIntoHole():"+msg);
 }
}

下面是新的PegAdapter,叫做two-way adapter:

public class PegAdapter implements IRoundPeg,ISquarePeg{
 private RoundPeg roundPeg;
 private SquarePeg squarePeg;  // 构造方法
 public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
 // 构造方法
 public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)  public void insert(String str){ roundPeg.insertIntoHole(str);}
}

还有一种叫Pluggable Adapters,可以动态的获取几个adapters中一个。使用Reflection技术,可以动态的发现类中的Public方法。

系列文章:

Java设计模式(1)工厂模式(Factory模式)

Java设计模式(2)单态模式(Singleton模式)

Java设计模式(3)建造者模式(Builder模式)

Java设计模式(4)原型模式(Prototype模式)

Java设计模式(5)共享模式/享元模式(Flyweight模式)

Java设计模式(6)桥模式(Bridge模式)

Java设计模式(7)装饰模式(Decorator模式)

Java设计模式(8)组合模式(Composite模式)

Java设计模式(9)适配器模式(Adapter模式)

Java设计模式(10)代理模式(Proxy模式)

Java设计模式(11)外观模式(Facade模式)

Java设计模式(12)迭代模式(Iterator模式)

Java设计模式(13)模板模式(Template模式)

Java设计模式(14)责任链模式(Chain of Responsibility模式)

Java设计模式(15)备忘录模式(Memento模式)

Java设计模式(16)中介模式(Mediator模式)

Java设计模式(17)解释器模式(Interpreter模式)

Java设计模式(18)策略模式(Strategy模式)

Java设计模式(19)状态模式(State模式)

Java设计模式(20)观察者模式(Observer模式)

Java设计模式(21)访问模式(Visitor者模式)

Java设计模式(22)命令模式(Command模式)

Java设计模式(9)适配器模式(Adapter模式)的更多相关文章

  1. Java设计模式之适配器模式(Adapter)

    转载:<JAVA与模式>之适配器模式 这个总结的挺好的,为了加深印象,我自己再尝试总结一下 1.定义: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法 ...

  2. java设计模式之六适配器模式(Adapter)

    适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题.主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式.首先,我们来看看类的适配器模 ...

  3. 夜话JAVA设计模式之适配器模式(adapter pattern)

    适配器模式:将一个类的接口,转换成客户期望的另一个接口,让不兼容的接口变成兼容. 1.类适配器模式:通过多重继承来实现适配器功能.多重继承就是先继承要转换的实现类,再实现被转换的接口. 2.对象适配器 ...

  4. Java设计模式之适配器模式(Adapter Pattern)

    Adapter Pattern的作用是在不改变功能的前提下转换接口.Adapter分为两类,一类是Object Adapter, 还有一类是Class Adapter.因为Class Adapter的 ...

  5. Java设计模式之接口型模式总结

    摘要: 原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6508967.html 之前认真学习了Java设计模式中的四大接口型模式,分别为:适 ...

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

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

  7. java设计模式5——适配器模式

    java设计模式5--适配器模式 1.结构型模式介绍 1.1.作用 从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题. 分类: 适配器模式 代理模式 桥接模式 装饰模式 组合模式 ...

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

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

  9. Java设计模式之《桥接模式》及应用场景

    摘要: 原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6497919.html 这里摘抄一份他处的概念,你可以不必理会,先看下面得讲解与实例, ...

  10. Java设计模式之《外观模式》及应用场景

    原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/6484128.html 1.外观模式简介 外观模式,一般用在子系统与访问之间,用于对访问屏蔽复 ...

随机推荐

  1. RabbitMQ 消费端 Client CPU 100%的解决办法

    Func<bool> run = () => { try { using (IConnection conn = cf.CreateConnection()) { using (IM ...

  2. kafka 配置文件注释

    文章转载自:http://liyonghui160com.iteye.com/blog/2163899 server.properties配置: server.properties中所有配置参数说明( ...

  3. Tomcat7启动报Error listenerStart错误

    问题 Tomcat7在启动时报错,详细信息如下: 十一月 23, 2013 7:21:58 下午 org.apache.catalina.core.StandardContext startInter ...

  4. 【Android开发】Android应用程序目录结构

    原文:http://android.eoe.cn/topic/summary Android开发之旅:组件生命周期吴秦 Android开发之旅:HelloWorld项目的目录结构 * HelloWor ...

  5. 互斥锁pthread_mutex_init()函数

    linux下为了多线程同步,通常用到锁的概念.posix下抽象了一个锁类型的结构:ptread_mutex_t.通过对该结构的操作,来判断资源是否可以访问.顾名思义,加锁(lock)后,别人就无法打开 ...

  6. mySql索引优化分析

    MySQL索引优化分析 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字 ...

  7. rdesktop 指定服务器的分频率

    rdesktop -uAdministrator -g 1265x728 10.100.0.225 &

  8. lua -- encode and decode

    json.encode 将表格数据编码为 JSON 字符串. 格式: jsonString = json.encode(表格对象) 用法示例: ,b=,c2=},d={,},}) echo(str) ...

  9. epoll, NIO,AIO

    Java 网络IO编程总结(BIO.NIO.AIO均含完整实例代码) Java中BIO,NIO和AIO使用样例 https://blog.insanecoder.top/javazhong-bio-n ...

  10. 15个Spring的核心注释示例

    众所周知,Spring DI和Spring IOC是Spring Framework的核心概念.让我们从org.springframework.beans.factory.annotation和org ...