原文:http://c.biancheng.net/view/1361.html
在现实生活中,经常出现两个对象因接口不兼容而不能在一起工作的实例,这时需要第三者进行适配。例如,讲中文的人同讲英文的人对话时需要一个翻译,用直流电的笔记本电脑接交流电源时需要一个电源适配器,用计算机访问照相机的 SD 内存卡时需要一个读卡器等。
在软件设计中也可能出现:需要开发的具有某种业务功能的组件在现有的组件库中已经存在,但它们与当前系统的接口规范不兼容,如果重新开发这些组件成本又很高,这时用适配器模式能很好地解决这些问题。
定义与特点
适配器模式(Adapter)的定义如下:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。适配器模式分为类结构型模式和对象结构型模式两种,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些。
该模式的主要优点如下。
- 客户端通过适配器可以透明地调用目标接口。
- 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。
- 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
其缺点是:对类适配器来说,更换适配器的实现过程比较复杂。
适配器模式的结构
适配器模式中一共有4个角色,分别是:(源接口)、目标接口、被适配类、适配器
其过程就是通过适配器将被适配类适配为目标角色的API。
适配器模式的形式分为:类的适配器模式 & 对象的适配器模式
类的适配器模式
类适配器模式是通过继承来实现适配功能的

程序代码
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
|
package adapter;
//目标接口
interface Target
{
public void request();
}
//适配者接口
class Adaptee
{
public void specificRequest()
{
System.out.println("适配者中的业务代码被调用!");
}
}
//类适配器类
class ClassAdapter extends Adaptee implements Target
{
public void request()
{
specificRequest();
}
}
//客户端代码
public class ClassAdapterTest
{
public static void main(String[] args)
{
System.out.println("类适配器模式测试:");
Target target = new ClassAdapter();
target.request();
}
}
// 运行结果
类适配器模式测试:
适配者中的业务代码被调用!
|
对象的适配器模式

程序代码
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
|
package adapter;
//对象适配器类
class ObjectAdapter implements Target
{
private Adaptee adaptee;
public ObjectAdapter(Adaptee adaptee)
{
this.adaptee=adaptee;
}
public void request()
{
adaptee.specificRequest();
}
}
//客户端代码
public class ObjectAdapterTest
{
public static void main(String[] args)
{
System.out.println("对象适配器模式测试:");
Adaptee adaptee = new Adaptee();
Target target = new ObjectAdapter(adaptee);
target.request();
}
}
// 运行结果
对象适配器模式测试:
适配者中的业务代码被调用!
|
应用实例
【例1】用适配器模式(Adapter)模拟新能源汽车的发动机。
分析:新能源汽车的发动机有电能发动机(Electric Motor)和光能发动机(Optical Motor)等,各种发动机的驱动方法不同,例如,电能发动机的驱动方法 electricDrive() 是用电能驱动,而光能发动机的驱动方法 opticalDrive() 是用光能驱动,它们是适配器模式中被访问的适配者。
客户端希望用统一的发动机驱动方法 drive() 访问这两种发动机,所以必须定义一个统一的目标接口 Motor,然后再定义电能适配器(Electric Adapter)和光能适配器(Optical Adapter)去适配这两种发动机。
我们把客户端想访问的新能源发动机的适配器的名称放在 XML 配置文件中,客户端可以通过对象生成器类 ReadXML 去读取。这样,客户端就可以通过 Motor 接口随便使用任意一种新能源发动机去驱动汽车,图 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
//适配者1:电能发动机
class ElectricMotor {
public void electricDrive() {
System.out.println("电能发动机驱动汽车!");
}
}
//适配者2:光能发动机
class OpticalMotor {
public void opticalDrive() {
System.out.println("光能发动机驱动汽车!");
}
}
//目标:发动机
interface Motor {
public void drive();
}
//电能适配器
class ElectricAdapter implements Motor {
private ElectricMotor emotor;
public ElectricAdapter() {
emotor = new ElectricMotor();
}
public void drive() {
emotor.electricDrive();
}
}
//光能适配器
class OpticalAdapter implements Motor {
private OpticalMotor omotor;
public OpticalAdapter() {
omotor = new OpticalMotor();
}
public void drive() {
omotor.opticalDrive();
}
}
//客户端代码
class MotorAdapterTest {
public static void main(String[] args) {
System.out.println("适配器模式测试:");
Motor motor = (Motor) ReadXML.getObject();
motor.drive();
}
}
// 运行结果
适配器模式测试:
新类名:com.eh.eden.pattern.ElectricAdapter
电能发动机驱动汽车!
|
config.xml
1
2
3
4
|
<?xml version="1.0" encoding="UTF-8"?>
<config>
<className>ElectricAdapter</className>
</config>
|
使用场景
适配器模式(Adapter)通常适用于以下场景。
- 以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致。
- 使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。
扩展
适配器模式(Adapter)可扩展为双向适配器模式,双向适配器类既可以把适配者接口转换成目标接口,也可以把目标接口转换成适配者接口,其结构图如下图所示。

程序代码如下:
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
60
61
62
63
64
65
66
67
68
69
70
71
|
package adapter;
//目标接口
interface TwoWayTarget
{
public void request();
}
//适配者接口
interface TwoWayAdaptee
{
public void specificRequest();
}
//目标实现
class TargetRealize implements TwoWayTarget
{
public void request()
{
System.out.println("目标代码被调用!");
}
}
//适配者实现
class AdapteeRealize implements TwoWayAdaptee
{
public void specificRequest()
{
System.out.println("适配者代码被调用!");
}
}
//双向适配器
class TwoWayAdapter implements TwoWayTarget,TwoWayAdaptee
{
private TwoWayTarget target;
private TwoWayAdaptee adaptee;
public TwoWayAdapter(TwoWayTarget target)
{
this.target=target;
}
public TwoWayAdapter(TwoWayAdaptee adaptee)
{
this.adaptee=adaptee;
}
public void request()
{
adaptee.specificRequest();
}
public void specificRequest()
{
target.request();
}
}
//客户端代码
public class TwoWayAdapterTest
{
public static void main(String[] args)
{
System.out.println("目标通过双向适配器访问适配者:");
TwoWayAdaptee adaptee=new AdapteeRealize();
TwoWayTarget target=new TwoWayAdapter(adaptee);
target.request();
System.out.println("-------------------");
System.out.println("适配者通过双向适配器访问目标:");
target=new TargetRealize();
adaptee=new TwoWayAdapter(target);
adaptee.specificRequest();
}
}
// 运行结果
目标通过双向适配器访问适配者:
适配者代码被调用!
-------------------
适配者通过双向适配器访问目标:
目标代码被调用!
|