适配器模式

本文最后更新于 2025年7月25日 中午

将类的接口转换成客户需要的另一个接口。使得原本由于接口不兼容而不能在一起工作的那些类可以一起工作

角色

1 目标角色 目标抽象类定义客户所需的接口,可以是一个抽象类或接口,也可以是具体类

2 适配者角色 适配者就是被适配的橘色,它定义了一个已经存在的接口,这个接口需要适配。

3 适配器角色 适配器可以调用另一个接口,作为一个转换器,完成适配者和目标中间的适配过程。

场景

1 系统需要使用现有的类,但现有的类不兼容

2 需要建立一个可以重复使用,用于一些彼此关系不大的类,并易于扩展,以便于面对将来会出现的类

3 需要一个统一的输出接口,但是输入类型却不可预知

简单来说 适配器模式就是通过一些手段完成对数据的组织和内容进行重新表示。

类适配器模式

类适配器模式主要是通过接口实现的方式,将原类结构以重写方法形式转换为目标所需的数据形式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 原始适配者
public class PowerAdaptee{
private int output = 220 ;
public int output220v(){
System.out.println("电源输出电压:" + output);
return output ;
}
}

// 目标角色
public interface PowerTarget {
int output5v();

}

// 适配器
public class PowerAdapter extends PowerAdaptee implements PowerTarget {
@Override
public int output5v(){
int output = super.output220v();
System.out.println("classadapter-适配器开始工作,输出电压是:" + output ) ;
output = output/44 ;
System.out.println("classadapter-适配工作完成,输出电压是:" + output ) ;
return output ;
}
}

// 调用
public class Test{
public static void main(String[] args){
PowerTarget target = new PowerAdapter();
target.output5v();
}
}

对象适配器

对于对象适配器,其实是通过组合的方式,拿到适配者的原始实例,实现目标方法,完成最终数据的组装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 适配者角色
public class PowerAdaptee {
private int output = 220 ;
public int output220V(){
System.out.println("电源输出电压:" + output);
return output ;
}
}

// 目标角色
public interface PowerTarget{
int output5v();
}

// 适配器角色
public class PowerAdapter implements PowerTarget{
private PowerAdaptee powerAdaptee;
public PowerAdapter(PowerAdaptee powerAdaptee){
this.powerAdaptee = powerAdaptee ;
}
@Override
public int output5v(){
int output = powerAdaptee.output220v();
System.out.println("objectadapter-适配器开始工作,输出电压是:" + output);
output = output/44 ;
System.out.println("objectadapter-适配工作完成,输出电压是:" + output ) ;
return output ;
}
}

// 调用
public class Test {
public static void main(String[] args){
PowerTarget target = new PowerAdapter(new PowerAdaptee());
target.output5v();
}
}

缺省适配器

当不需要全部实现接口提供的方法时,可以设计一个适配器抽象类实现接口,并未接口中的每个方法提供默认方法,抽象类的子类就可以有选择的覆盖父类的某些方法实现需求。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 适配者角色
public class PowerAdaptee {
private int output = 220 ;
public int output220V(){
System.out.println("电源输出电压:" + output);
return output ;
}
}

// 目标角色
public interface PowerTarget{
int output5v();
int output8v();
int output12v();
}

// 适配器
public abstract class PowerAdapter implements PowerTarget{
protected PowerAdaptee powerAdaptee ;
public PowerAdapter(PowerAdaptee powerAdaptee){
this.powerAdaptee = powerAdaptee ;
}
@Override
public int output5v(){
return 0 ;
}
@Override
public int output9v(){
return 0 ;
}
@Override
public int output12v(){
return 0 ;
}
}

// 实现,在具体实现类中重写方法完成适配
public class Power5vAdapter extends PowerAdapter {
public Power5vAdapter(PowerAdaptee powerAdaptee){
super(powerAdaptee);
}

@Override
public int output5v(){
int output = powerAdaptee.output220v();
System.out.println("defaultadapter-适配器开始工作,输出电压是:" + output);
output = output/44 ;
System.out.println("defaultadapter-适配工作完成,输出电压是:" + output ) ;
return output ;
}
}

// 调用
public class Test{
public static void main(String[] args){
PowerTarget target = new Power5vAdapter(new PowerAdaptee());
target.output5v();
}
}

总结

适配器模式作为结构型设计模式的典型代表,解决了 接口不兼容导致无法复用的问题 ,使得原本互不兼容的类可以协同工作。通过 类适配器(继承)、对象适配器(组合)与缺省适配器(抽象类)三种形式,它为不同场景下的适配需求提供了解决思路。实际开发中,我们更推荐使用对象适配器模式 ,因其遵循了组合优于继承的设计原则,具有更好的扩展性与灵活性。而缺省适配器则常用于接口方法过多但只需实现部分方法的场景,降低实现成本。掌握适配器模式,不仅有助于提升系统的 兼容性与灵活性 ,也体现了对面向对象开闭原则与复用性的深刻理解。


适配器模式
http://gadoid.io/2025/07/25/适配器模式/
作者
Codfish
发布于
2025年7月25日
许可协议