适配器动态响应测试要怎么设置(设计模式-结构型-适配器模式)

1.概念
  • 将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作;
  • 适配器模式分为类适配器模式和对象适配器模式,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些;
2.结构
  • 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口;
  • 适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口;
  • 适配器(Adapter)类:它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。
3.类适配器模式

实现方式:定义一个适配器类来实现当前系统的业务接口,同时又继承现有组件库中已经存在的组件;

示例:读卡器

适配器动态响应测试要怎么设置(设计模式-结构型-适配器模式)(1)

(1)目标接口-SD卡

public interface SDCard { //从SD卡中读取数据 String readSD(); //往SD卡中写数据 void writeSD(String msg); }

(2)具体的实现类

public class SDCardImpl implements SDCard { public String readSD() { String msg = "SDCard read msg : hello word SD"; return msg; } public void writeSD(String msg) { System.out.println("SDCard write msg :" msg); } }

(3)计算机类

public class Computer { //从SD卡中读取数据 public String readSD(SDCard sdCard) { if(sdCard == null) { throw new NullPointerException("sd card is not null"); } return sdCard.readSD(); } }

(4)适配者类的接口

public interface TFCard { //从TF卡中读取数据 String readTF(); //往TF卡中写数据 void writeTF(String msg); }

实现类:

public class TFCardImpl implements TFCard { public String readTF() { String msg = "TFCard read msg : hello word TFcard"; return msg; } public void writeTF(String msg) { System.out.println("TFCard write msg :" msg); } }

(5)适配器类

public class SDAdapterTF extends TFCardImpl implements SDCard { public String readSD() { System.out.println("adapter read tf card"); return readTF(); } public void writeSD(String msg) { System.out.println("adapter write tf card"); writeTF(msg); } }

(6)测试类

public class Client { public static void main(String[] args) { //创建计算机对象 Computer computer = new Computer(); //读取SD卡中的数据 String msg = computer.readSD(new SDCardImpl()); System.out.println(msg); System.out.println("==============="); //使用该电脑读取TF卡中的数据 //定义适配器类 String msg1 = computer.readSD(new SDAdapterTF()); System.out.println(msg1); } }

运行结果:

适配器动态响应测试要怎么设置(设计模式-结构型-适配器模式)(2)

  • 类适配器模式违背了合成复用原则(通过将已有的对象纳入新对象中,作为新对象的成员对象来实现的,新对象可以调用已有对象的功能,从而达到复用)。类适配器是客户类有一个接口规范的情况下可用,反之不可用;
  • 关于合成复用原则的小示例:

适配器动态响应测试要怎么设置(设计模式-结构型-适配器模式)(3)

可以看出用继承关系实现会产生很多子类,而且增加新的“动力源”或者增加新的“颜色”都要修改源代码,这违背了开闭原则,显然不可取。但如果改用组合关系实现就能很好地解决以上问题:

适配器动态响应测试要怎么设置(设计模式-结构型-适配器模式)(4)

4.对象适配器模式

实现方式:对象适配器模式可釆用将现有组件库中已经实现的组件引入适配器类中,该类同时实现当前系统的业务接口;

示例:读卡器

适配器动态响应测试要怎么设置(设计模式-结构型-适配器模式)(5)

类适配器模式的代码,我们只需要修改适配器类(SDAdapterTF)和测试类;

(1)适配器类

public class SDAdapterTF implements SDCard { //声明适配者类 private TFCard tfCard; public SDAdapterTF(TFCard tfCard) { this.tfCard = tfCard; } public String readSD() { System.out.println("adapter read tf card"); return tfCard.readTF(); } public void writeSD(String msg) { System.out.println("adapter write tf card"); tfCard.writeTF(msg); } }

(2)测试类

public class Client { public static void main(String[] args) { //创建计算机对象 Computer computer = new Computer(); //读取SD卡中的数据 String msg = computer.readSD(new SDCardImpl()); System.out.println(msg); System.out.println("==============="); //使用该电脑读取TF卡中的数据 //创建适配器类对象 SDAdapterTF sdAdapterTF = new SDAdapterTF(new TFCardImpl()); String msg1 = computer.readSD(sdAdapterTF); System.out.println(msg1); } }

注意:还有一个适配器模式是接口适配器模式。当不希望实现一个接口中所有的方法时,可以创建一个抽象类Adapter,实现所有方法。而此时我们只需要继承该抽象类即可。

5.应用场景
  • 以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致;
  • 使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同;
6.JDK源码解析
  • Reader(字符流)、InputStream(字节流)的适配使用的是InputStreamReader;
  • InputStreamReader继承自java.io包中的Reader,对他中的抽象的未实现的方法给出实现;

public int read() throws IOException { return sd.read(); } public int read(char cbuf[], int offset, int length) throws IOException { return sd.read(cbuf, offset, length); }

如上代码中的sd(StreamDecoder类对象),在Sun的JDK实现中,实际的方法实现是对sun.nio.cs.StreamDecoder类的同名方法的调用封装。类结构图如下

适配器动态响应测试要怎么设置(设计模式-结构型-适配器模式)(6)

  • InputStreamReader是对同样实现了Reader的StreamDecoder的封装;
  • StreamDecoder不是Java SE API中的内容,是Sun JDK给出的自身实现。但我们知道他们对构造方法中的字节流类(InputStream)进行封装,并通过该类进行了字节流和字符流之间的解码转换;
  • StreamDecoder采用了适配器模式;
,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页