结构型-适配器模式
2026/1/15大约 3 分钟Java设计模式结构型模式适配器模式Java设计模式面向对象架构设计最佳实践代码优化
概述
适配器模式(Adapter Pattern)将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适用场景
- 想使用一个已存在的类,但其接口不符合需求
- 想创建一个可复用的类,与不相关或不可预见的类协同工作
- 需要使用几个现有的子类,但不可能对每一个都进行子类化以匹配接口
角色组成
- Target(目标接口):客户端期望的接口
- Adaptee(被适配者):需要被适配的类
- Adapter(适配器):将Adaptee转换为Target接口
类适配器(继承方式)
// 目标接口
public interface Target {
void request();
}
// 被适配者
public class Adaptee {
public void specificRequest() {
System.out.println("被适配者的方法");
}
}
// 类适配器(继承)
public class ClassAdapter extends Adaptee implements Target {
@Override
public void request() {
specificRequest();
}
}
// 客户端
public class Client {
public static void main(String[] args) {
Target target = new ClassAdapter();
target.request();
}
}# 输出
被适配者的方法对象适配器(组合方式)
// 对象适配器(组合)
public class ObjectAdapter implements Target {
private Adaptee adaptee;
public ObjectAdapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
// 客户端
public class Client {
public static void main(String[] args) {
Adaptee adaptee = new Adaptee();
Target target = new ObjectAdapter(adaptee);
target.request();
}
}类图
实际案例:电压适配器
// 220V 电压(被适配者)
public class Voltage220V {
public int output220V() {
System.out.println("输出 220V 电压");
return 220;
}
}
// 5V 电压接口(目标接口)
public interface Voltage5V {
int output5V();
}
// 电压适配器
public class VoltageAdapter implements Voltage5V {
private Voltage220V voltage220V;
public VoltageAdapter(Voltage220V voltage220V) {
this.voltage220V = voltage220V;
}
@Override
public int output5V() {
int src = voltage220V.output220V();
int dst = src / 44; // 转换
System.out.println("适配器转换: " + src + "V -> " + dst + "V");
return dst;
}
}
// 手机
public class Phone {
public void charge(Voltage5V voltage5V) {
int v = voltage5V.output5V();
if (v == 5) {
System.out.println("电压正常,开始充电");
} else {
System.out.println("电压异常,无法充电");
}
}
}
// 使用
public class Client {
public static void main(String[] args) {
Phone phone = new Phone();
phone.charge(new VoltageAdapter(new Voltage220V()));
}
}# 输出
输出 220V 电压
适配器转换: 220V -> 5V
电压正常,开始充电接口适配器(缺省适配器)
// 多方法接口
public interface ServiceInterface {
void method1();
void method2();
void method3();
}
// 抽象适配器(提供默认空实现)
public abstract class AbstractServiceAdapter implements ServiceInterface {
@Override
public void method1() {}
@Override
public void method2() {}
@Override
public void method3() {}
}
// 只需要实现需要的方法
public class ConcreteService extends AbstractServiceAdapter {
@Override
public void method1() {
System.out.println("只实现 method1");
}
}JDK 中的应用
java.util.Arrays#asList()- 数组转Listjava.io.InputStreamReader- 字节流转字符流java.io.OutputStreamWriter- 字符流转字节流javax.xml.bind.annotation.adapters.XmlAdapter
Spring 中的应用
// SpringMVC 中的 HandlerAdapter
public interface HandlerAdapter {
boolean supports(Object handler);
ModelAndView handle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception;
}优缺点
优点:
- 可以让任何两个没有关联的类一起运行
- 提高了类的复用
- 增加了类的透明度
- 灵活性好
缺点:
- 过多使用适配器会让系统凌乱
- 类适配器只能适配一个类
- 对象适配器需要额外的对象引用
类适配器 vs 对象适配器
| 特性 | 类适配器 | 对象适配器 |
|---|---|---|
| 实现方式 | 继承 | 组合 |
| 灵活性 | 较低 | 较高 |
| 适配多个类 | 不支持 | 支持 |
| 重写方法 | 可以 | 不可以 |
| 推荐程度 | 一般 | 推荐 |
