# 适配器模式

适配器模式是一种结构型设计模式，它让接口不兼容的对象能够协同工作：适配器类实现客户端所期望的接口，并将每次调用转换为现有类（即被适配者）实际提供的形式。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup> 四人组——埃里希·伽玛（Erich Gamma）、理查德·赫尔姆（Richard Helm）、拉尔夫·约翰逊（Ralph Johnson）和约翰·弗利赛德斯（John Vlissides）——在1994年的著作《设计模式：可复用面向对象软件的基础》（*Design Patterns: Elements of Reusable Object-Oriented Software*）中收录了该模式，他们将其意图表述为：“将一个类的接口转换成客户希望的另外一个接口。适配器让原本由于接口不兼容而不能一起工作的类可以一起工作。”<sup>[2](https://www.acronos.com/DesignPatterns/adapter.html)</sup> 该模式也称为包装器（Wrapper）。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup>

| 关键事实 | 详情 |
|---|---|
| 类别 | 四人组目录中的结构型模式；亦称包装器<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup> |
| 参与者 | 目标（客户端使用的接口）、客户端、被适配者（需要适配的现有接口）、适配器<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup> |
| 两种形式 | 类适配器（继承，需要多重继承）和对象适配器（组合与委托）<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup> |
| 典型触发场景 | 一个现有类（通常是第三方或遗留代码），其接口与客户端需求不匹配，且源代码无法修改<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup> |
| Java 示例 | `Arrays.asList()`、`Collections.list()`、`InputStreamReader`、`OutputStreamWriter`、`XmlAdapter`<sup>[4](https://refactoring.guru/design-patterns/adapter/java/example)</sup> |
| 主要成本 | 每次适配多一个类，外加每次操作上的委托调用<sup>[5](https://www.wihlidal.com/files/getd_full_book.pdf)</sup> |
| 现代默认选择 | 对象适配器，因为它在单继承语言中可用，且可在运行时重新配置<sup>[6](https://tobiasduerschmid.github.io/SEBook/designpatterns/adapter.html)</sup> |

## 目的与适用场景

适配器是一种修补手段，而非设计手段。它适用于你所能控制的代码必须与你无法控制的代码对接的情形：供应商库、遗留模块、收购的产品，或你所依赖却发生了变化的 API。<sup>[2](https://www.acronos.com/DesignPatterns/adapter.html)</sup> 四人组列出了三种情况：你想使用一个现有类，但其接口与你需要的接口不匹配；你想要一个可复用的类，能与不相关或不可预见的类协作；以及仅适用于对象适配器的情况——你需要使用多个现有子类，但通过子类化逐一适配并不现实。<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup>

机制很简单。适配器实现客户端的接口，通过该接口接收调用，并将每个请求以被包装对象所期望的格式和顺序传递给它。被包装对象对适配器一无所知，而客户端代码只要通过接口工作就不会与具体适配器类耦合，因此可以在不修改客户端的情况下引入新的适配器。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup> 如果这些类是从头设计的，通常更适合采用桥接等其他模式；“已经存在”这一条件正是适配器所解决问题的区别性特征。<sup>[2](https://www.acronos.com/DesignPatterns/adapter.html)</sup>

**适配的工作量差异很大。** 工作范围从简单的重命名——例如 `quack()` 直接映射到 `gobble()`——到数据转换，如单位换算、数据结构重格式化或协议间转换，再到行为适配——被适配者的行为根本不同，适配器必须添加逻辑来弥合语义鸿沟。<sup>[6](https://tobiasduerschmid.github.io/SEBook/designpatterns/adapter.html)</sup> 四人组描述了同样的谱系，从更改操作名称到支持一整套完全不同的操作，并指出工作量取决于目标接口与被适配者接口的相似程度。<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup> 一个经典的示例是将方钉适配到圆孔：适配器继承 `RoundPeg`，包装一个 `SquarePeg`，并报告一个半径，等于钉子的宽度乘以二的平方根再除以二，即包含该正方形的最小圆的半径。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup>

## 类适配器与对象适配器

两种实现在适配器如何获取被适配者的行为上有所不同。<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup>

类适配器同时继承目标和被适配者，用一个继承分支获取接口，用另一个分支获取实现；在 C++ 中，通常的惯例是公有继承接口、私有继承实现。<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup> 它通过重写方法来适配，无需包装，只引入一个对象，且到达被适配者无需指针间接寻址。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup><sup> • </sup><sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup> 其局限性相当显著：它绑定于一个具体的被适配者类，因此无法将该类与其所有子类一起适配；它要求类的多重继承，C++ 允许但 Java、C# 和 [TypeScript](https://www.edgepedia.org/zh/typescript) 不允许；并且它使适配器与被适配类紧密耦合。<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup><sup> • </sup><sup>[7](https://cleancode.dosibridge.com/articles/structural-patterns/adapter/)</sup><sup> • </sup><sup>[8](https://www.itlead.org/interview-questions/patterns/adapter-pattern-types)</sup> 在单继承语言中，只有当目标是接口时类适配器才可行，这样适配器才能继承被适配者并实现目标。<sup>[6](https://tobiasduerschmid.github.io/SEBook/designpatterns/adapter.html)</sup> 具体到 Java，许多库类是 `final` 的或并非为继承而设计，继承实现可能暴露超出目标接口应允许范围的更多行为。<sup>[9](https://softwarepatternslexicon.com/java/structural-patterns/adapter-pattern/)</sup>

对象适配器持有对被适配者的引用并进行委托，将 `request()` 实现为对被适配者 `specificRequest()` 的调用。编写转发调用需要少许额外工作，但它避免了多重继承带来的所有问题：不破坏封装、没有菱形问题、无需担心编译器支持。<sup>[2](https://www.acronos.com/DesignPatterns/adapter.html)</sup> 单个对象适配器可以与被适配者及其所有子类一起工作，并且可以在运行时指向任何子类，这是继承版本做不到的。<sup>[2](https://www.acronos.com/DesignPatterns/adapter.html)</sup><sup> • </sup><sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup> 其唯一的结构性劣势是重写被适配者行为更难：需要子类化被适配者并让适配器引用该子类。<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup>

现代实践倾向于对象适配器。它可以与被适配者的任何子类组合，可在运行时重新配置，且不要求任何一方为继承而开放——这一立场与约书亚·布洛赫（Joshua Bloch）在《Effective Java》（第18条）中“优先使用组合而非继承”的建议一致。<sup>[6](https://tobiasduerschmid.github.io/SEBook/designpatterns/adapter.html)</sup> 在 Java 中，对象适配器通常是首选，因为它们依赖组合，顺应而非对抗该语言的单继承限制；只有在被适配者被刻意设计为可扩展、且继承已经提供了最自然的定制方式时，选择类适配器才有意义。<sup>[9](https://softwarepatternslexicon.com/java/structural-patterns/adapter-pattern/)</sup>

第三种形式是双向适配器，可以在两个方向上转换调用。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup> 四人组还描述了可插拔适配器——内建接口适配的类，他们将该术语归功于 Object-Works\[Smalltalk](https://www.edgepedia.org/zh/smalltalk)；将适配内建于类中，消除了其他类只能看到某一固定接口的假设。<sup>[3](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)</sup>

## 与相关模式的区别

若干结构型模式都包装对象，区别在于接口发生了什么变化。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup>

- **外观（Facade）** 为整个对象子系统定义一个新的、简化的接口，而适配器使现有接口可用，通常只包装一个对象。适配器修复两个接口之间的不匹配；外观简化对接口本已正常工作的子系统的访问。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup><sup> • </sup><sup>[10](https://learning.jdfive.com/design-patterns/learn/adapter-pattern/)</sup> 如果外来接口与目标之间的差异非常大，应考虑使用外观而非简单适配器。<sup>[11](https://www.pmi.org/microsites/disciplined-agile/the-design-patterns-repository/the-adapter-pattern)</sup>
- **装饰器（Decorator）** 保持接口不变或扩展接口，并支持递归组合；适配器提供完全不同的接口，不支持递归组合。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup>
- **代理（Proxy）** 提供具有相同接口的替代品，控制访问；使用适配器时，调用者通过不同的接口访问对象。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup>
- **桥接（Bridge）** 通常在设计初期就规划好，使应用程序各部分可以独立变化，而适配器通常应用于现有应用，使原本不兼容的类协同工作。<sup>[1](https://refactoring.guru/design-patterns/adapter)</sup>

一种常见的误用是让适配器积累业务逻辑，而不是保持为纯粹的转换层。<sup>[10](https://learning.jdfive.com/design-patterns/learn/adapter-pattern/)</sup>

## 适配器在实践中的出现

标准库经常使用适配器。Java 核心库包括 `java.util.Arrays#asList()`、`java.util.Collections#list()`、`java.io.InputStreamReader(InputStream)`（返回一个 `Reader` 对象）、`java.io.OutputStreamWriter(OutputStream)` 以及 `javax.xml.bind.annotation.adapters.XmlAdapter`。<sup>[4](https://refactoring.guru/design-patterns/adapter/java/example)</sup> 在 C# 中，`StreamWriter` 将字节流适配为 `TextWriter` 接口，使面向文本的代码可以写入文件、[网络套接字](https://www.edgepedia.org/zh/network-socket)或内存而无需了解其差异，而 .NET 的数据库连接功能使用适配器在不同数据库引擎之上呈现一致的基础接口。<sup>[7](https://cleancode.dosibridge.com/articles/structural-patterns/adapter/)</sup><sup> • </sup><sup>[5](https://www.wihlidal.com/files/getd_full_book.pdf)</sup> 从托管应用程序导出 COM 接口，以及 [Windows Forms](https://www.edgepedia.org/zh/windows-forms) 本身作为 CLR 兼容语言与过程式 Win32 控件之间的桥梁，都可以被解读为该模式的隐式实例。<sup>[5](https://www.wihlidal.com/files/getd_full_book.pdf)</sup>

在企业集成中，通道适配器（Channel Adapter）将应用程序连接到消息系统：它访问应用程序的 API 或数据并在通道上发布消息，同样也接收消息并调用应用程序内部的功能，对消息系统而言充当消息客户端，并通过应用程序提供的接口调用应用程序功能。<sup>[12](https://www.enterpriseintegrationpatterns.com/patterns/messaging/ChannelAdapter.html)</sup>

该模式还可扩展到整个库的层面。2024 年一种名为 adaptoring 的研究方法使用适配器模式，将新 API 作为包装库提供，内部调用原始库，使新库能够免费获得原始库的缺陷修复和性能改进；作者确定了哪些 API 转换（如移除、重命名、参数重排和前置条件强制）可以自动生成，为 Python 实现了该方法，并在 SANER 2024 上展示。<sup>[13](https://arxiv.org/abs/2401.07053)</sup>

## 成本与测试

适配器是一种低成本方案：代价是多一个类，好处包括对现有行为的封装复用、通过向上转型实现与外来类的多态，以及对开闭原则的支持——因为可以在不破坏现有客户端代码的情况下引入新的适配器类型。<sup>[11](https://www.pmi.org/microsites/disciplined-agile/the-design-patterns-repository/the-adapter-pattern)</sup><sup> • </sup><sup>[1](https://refactoring.guru/design-patterns/adapter)</sup> 存在性能开销，因为在被适配者上调用的每个方法都必须先经过适配器。<sup>[5](https://www.wihlidal.com/files/getd_full_book.pdf)</sup> 在测试方面，可以用模拟对象替代外来对象，返回可预测的行为供适配器转换，并记录适配器对被适配者做了什么。<sup>[11](https://www.pmi.org/microsites/disciplined-agile/the-design-patterns-repository/the-adapter-pattern)</sup> 实践指导是：默认使用对象适配器，让每个适配器专注于一个被适配者和一个目标接口，并记录正在适配的内容。<sup>[10](https://learning.jdfive.com/design-patterns/learn/adapter-pattern/)</sup>

## References

1. [Adapter, Refactoring.Guru](https://refactoring.guru/design-patterns/adapter)
2. [Adapter · Design Patterns · Brian Davis, Acronos](https://www.acronos.com/DesignPatterns/adapter.html)
3. [Applicability | Design Patterns: Adapter, InformIT](https://www.informit.com/articles/article.aspx?p=1398600&seqNum=2)
4. [Adapter in Java, Refactoring.Guru](https://refactoring.guru/design-patterns/adapter/java/example)
5. [Game Development and Debugging (adapter pattern chapter), wihlidal.com](https://www.wihlidal.com/files/getd_full_book.pdf)
6. [Adapter Pattern · SE Book, Tobias Dürschmid](https://tobiasduerschmid.github.io/SEBook/designpatterns/adapter.html)
7. [Adapter Pattern: The Travel Plug Trick, CleanCode Mastery](https://cleancode.dosibridge.com/articles/structural-patterns/adapter/)
8. [What types of adapters exist?, ITLead](https://www.itlead.org/interview-questions/patterns/adapter-pattern-types)
9. [Adapter Pattern for Java Dependency Boundaries, Software Patterns Lexicon](https://softwarepatternslexicon.com/java/structural-patterns/adapter-pattern/)
10. [Adapter Pattern · Design Patterns, learning.jdfive.com](https://learning.jdfive.com/design-patterns/learn/adapter-pattern/)
11. [The Adapter Pattern, PMI Disciplined Agile Design Patterns Repository](https://www.pmi.org/microsites/disciplined-agile/the-design-patterns-repository/the-adapter-pattern)
12. [Channel Adapter, Enterprise Integration Patterns](https://www.enterpriseintegrationpatterns.com/patterns/messaging/ChannelAdapter.html)
13. [Adaptoring: Adapter Generation to Provide an Alternative API for a Library, arXiv:2401.07053](https://arxiv.org/abs/2401.07053)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software design and architecture › Titles 4 to Mo

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/adapter-pattern

English original: https://www.edgepedia.org/adapter-pattern
