1.概念2.结构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源码解析

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)

,