Java设计模式系列(6):适配器模式

布鸽不鸽 Lv4

前言

img

将一个类的接口,替换成客户端希望的另外一个接口,使原本因接口不兼容的类能一起工作,这就是适配器模式。

原文地址:https://xuedongyun.cn/post/26888/

适配器模式的角色

适配器模式(Adapter)包含以下主要角色

  • 目标(Target)接口:当前业务所期待的接口
  • 适配者(Adapter)类:当前组件的接口
  • 适配器(Adapter)类:一个转换器,把适配者接口,转化为目标接口

类适配器模式

实现方法:定义一个适配器类来实现当前业务接口,同时又继承原有组件

  • 假如我们的电脑只能读取SD卡,读取TF卡就需要适配器模式
1
2
3
4
5
6
7
8
9
public class Computer {

public String readSD(SDCard sdCard) {
if(sdCard == null) {
throw new NullPointerException("sd card null");
}
return sdCard.readSD();
}
}
  • SD卡的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface SDCard {
String readSD();
void writeSD(String msg);
}

// SD卡实现类
public class SDCardImpl implements SDCard {
public String readSD() {
String msg = "sd card read a msg :hello word SD";
return msg;
}

public void writeSD(String msg) {
System.out.println("sd card write msg : " + msg);
}
}
  • TF卡的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public interface TFCard {
String readTF();
void writeTF(String msg);
}

//TF卡实现类
public class TFCardImpl implements TFCard {

public String readTF() {
String msg ="tf card read msg : hello word tf card";
return msg;
}

public void writeTF(String msg) {
System.out.println("tf card write a msg : " + msg);
}
}
  • 定义适配器类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 定义适配器类(用SD兼容TF)
public class SDAdapterTF extends TFCardImpl implements SDCard {
@Override
public String readSD() {
System.out.println("adapter read tf card ");
return readTF();
}

@Override
public void writeSD(String msg) {
System.out.println("adapter write tf card");
writeTF(msg);
}
}
  • 使用
1
2
SDAdapterTF adapter = new SDAdapterTF();
computer.readSD(adapter);

对象适配器模式

实现方式:该类实现当前业务接口,同时将现有组件引入适配器类中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//创建适配器对象(SD兼容TF)
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);
}
}

应用场景

  1. 以前的组件满足功能需求,但是接口定义不一致
  2. 第三方开发的组件,接口定义不一致

JDK源码-InputStreamReader

在JDK中,使用InputStreamReader,进行从InputStream(字节流)到Reader(字符流)的转换。

  • InputStreamReader继承了抽象类Reader
  • InputStreamReader内部使用了StreamDecoder来执行实际的功能(抽象类Reader的具体实现)
    • 不是Java SE API中的内容,是Sun JDK给出的实现
    • 对构造方法中的字节流类(InputStream)进行封装,并通过该类进行了字节流和字符流之间的解码转换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class InputStreamReader extends Reader {

private final StreamDecoder sd;

public int read() throws IOException {
return sd.read();
}

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

// ...
}
img

结论:

  • 表层来看,InputStreamReader做了InputStream(字节流)到Reader(字符流)之间的转换
  • 实际上,是StreamDecoder的设计实现,采用了适配器模式
  • 标题: Java设计模式系列(6):适配器模式
  • 作者: 布鸽不鸽
  • 创建于 : 2023-09-07 15:07:38
  • 更新于 : 2023-09-07 17:21:02
  • 链接: https://xuedongyun.cn//post/26888/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论