参考:http://c.biancheng.net/view/1361.html
什么是适配器模式
适配器(Adapter)模式的定义:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。适配器模式分为类结构型模式和对象结构型模式两种,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些。
优点:
- 客户端通过适配器可以透明地调用目标接口。
- 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。
- 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
- 在很多业务场景中符合开闭原则。
缺点:
- 适配器编写过程需要结合业务场景全面考虑,可能会增加系统的复杂性。
- 增加代码阅读难度,降低代码可读性,过多使用适配器会使系统代码变得凌乱。
结构与实现
适配器模式(Adapter)包含以下主要角色。
- 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
- 适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。
- 适配器(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
| 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 29 30 31 32 33 34 35 36 37
| interface Target { public void request(); }
class Adaptee { public void specificRequest() { System.out.println("适配者中的业务代码被调用!"); } }
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 2
| 对象适配器模式测试: 适配者中的业务代码被调用!
|
应用实例
【例1】用适配器模式(Adapter)模拟新能源汽车的发动机。
分析:新能源汽车的发动机有电能发动机(Electric Motor)和光能发动机(Optical Motor)等,各种发动机的驱动方法不同,例如,电能发动机的驱动方法 electricDrive() 是用电能驱动,而光能发动机的驱动方法 opticalDrive() 是用光能驱动,它们是适配器模式中被访问的适配者。
客户端希望用统一的发动机驱动方法 drive() 访问这两种发动机,所以必须定义一个统一的目标接口 Motor,然后再定义电能适配器(Electric Adapter)和光能适配器(Optical 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
| interface Motor { public void drive(); }
class ElectricMotor { public void electricDrive() { System.out.println("电能发动机驱动汽车!"); } }
class OpticalMotor { public void opticalDrive() { System.out.println("光能发动机驱动汽车!"); } }
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(); } }
public class MotorAdapterTest { public static void main(String[] args) { System.out.println("适配器模式测试:"); Motor motor=new ElectricAdapter(); motor.drive(); } }
|
运行结果:
应用场景
适配器模式(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
| 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(); } }
|
运行结果:
1 2 3 4 5
| 目标通过双向适配器访问适配者: 适配者代码被调用! ------------------- 适配者通过双向适配器访问目标: 目标代码被调用!
|