# 反模式

反模式（anti-pattern）是针对常见软件问题的一种反复出现的解决方案，采用时看似好主意，却会带来麻烦；人们对其加以描述和命名，以便团队能够识别并讨论它。该术语由C++社区的成员安德鲁·柯尼希（Andrew Koenig）在1995年发表于《面向对象编程杂志》（*Journal of Object-Oriented Programming*）的一篇文章中提出；英国软件工程师、《重构》（*Refactoring*）的作者[马丁·福勒](https://www.edgepedia.org/zh/martin-fowler-software-engineer)（Martin Fowler）将柯尼希的定义概括为：“就像模式一样，只不过它给出的不是解决方案，而是某种表面上看似解决方案、实则不然的东西。”<sup>[1](https://martinfowler.com/bliki/AntiPattern.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 由安德鲁·柯尼希在《面向对象编程杂志》第8卷第1期（1995年3/4月）上提出；后收录于琳达·赖辛（Linda Rising）主编的《模式手册》（*The Patterns Handbook*）<sup>[1](https://martinfowler.com/bliki/AntiPattern.html)</sup> |
| 核心思想 | 开始时看似好主意，却会把你带入麻烦；正是其表面上的合理性使其具有危险性<sup>[1](https://martinfowler.com/bliki/AntiPattern.html)</sup> |
| 与坏味道的区别 | 反模式描述的是对项目有负面影响的反复出现的情境，而坏味道是设计不佳的症状<sup>[2](https://insights.sei.cmu.edu/blog/when-your-softwares-check-engine-light-is-on-identifying-design-problems-that-impact-software-failure/)</sup> |
| 范围 | 涵盖项目管理、架构和开发，通常表明组织与流程上的困难<sup>[2](https://insights.sei.cmu.edu/blog/when-your-softwares-check-engine-light-is-on-identifying-design-problems-that-impact-software-failure/)</sup> |
| 命名目录 | 菲利普·A·拉普兰特（Phillip A. Laplante）与科林·J·尼尔（Colin J. Neill）所著《反模式：识别、重构与管理》（*AntiPatterns: Identification, Refactoring, and Management*）编录了各类组织中有缺陷的流程与实现<sup>[3](https://adtmag.com/articles/2001/06/19/antipatterns.aspx)</sup><sup> • </sup><sup>[11](https://pure.psu.edu/en/publications/antipatterns-identification-refactoring-and-management/)</sup> |
| 实测影响 | 反模式显著影响维护期间编辑文件所需的开发者工作量<sup>[4](https://arxiv.org/abs/2009.02438)</sup> |
| 情境依赖性 | 同一解决方案在某些情境下可以是好模式，在另一些情境下则可能是反模式<sup>[1](https://martinfowler.com/bliki/AntiPattern.html)</sup> |

## 起源与定义

柯尼希的文章发表于《面向对象编程杂志》1995年3/4月号，后收录于琳达·赖辛主编的《模式手册》。由于该论文无法在线获取，福勒凭记忆写道，他记得其核心思想是：某种开始时看似好主意、却会把你带入麻烦的东西。<sup>[1](https://martinfowler.com/bliki/AntiPattern.html)</sup>

这一定义有意与设计模式的概念相呼应：设计模式记录的是针对反复出现的问题的、经过验证的具名解决方案，以便他人套用。反模式记录的则是其镜像：一条反复出现的错误岔路。福勒认为，这一原初的聚焦点比现代更宽泛的用法更有用——后者将该词仅仅理解为任何糟糕的主意。真正的反模式与单纯的糟糕做法之间的区别在于：它看上去像一个好的解决方案，于是明理之人也走上了那条路，只有在投入了相当多的努力之后，糟糕的结果才显现出来。<sup>[1](https://martinfowler.com/bliki/AntiPattern.html)</sup>

## 反模式、坏味道与技术债务

软件工程文献将反模式列为设计问题的若干相关指标之一。[卡内基梅隆大学](https://www.edgepedia.org/zh/carnegie-mellon-university)软件工程研究所（SEI）的研究者将反模式定义为反复出现、损害软件项目的情境，与标示设计缺陷的坏味道相对；反模式横跨项目管理、架构与开发，通常指向组织结构与流程方面的问题。<sup>[2](https://insights.sei.cmu.edu/blog/when-your-softwares-check-engine-light-is-on-identifying-design-problems-that-impact-software-failure/)</sup> “代码坏味道”（code smell）一词由[肯特·贝克](https://www.edgepedia.org/zh/kent-beck)（Kent Beck）与马丁·福勒于1999年在《重构》中提出，指直觉上像是糟糕方案、并暗示存在改进可能的实现结构；对大多数代码坏味道而言，都有已知的重构方案。<sup>[2](https://insights.sei.cmu.edu/blog/when-your-softwares-check-engine-light-is-on-identifying-design-problems-that-impact-software-failure/)</sup>

反模式与技术债务的概念密切相关。技术债务是指那些追求初期快速收益、长期却适得其反的设计与实现选择的累积。在SEI一项关于技术债务的实地研究中，架构选择被从业者列为技术债务的首要来源，65%的受访者表示没有明确的技术债务管理实践。<sup>[5](https://www.sei.cmu.edu/blog/a-field-study-of-technical-debt/)</sup> 在相互关联的文件之间传播缺陷的有缺陷的架构结构被称为“架构根源”（architecture roots），这是一种会带来高昂维护代价、且清除它需要重构的技术债务。<sup>[6](https://www.sei.cmu.edu/library/identifying-the-architectural-roots-of-vulnerabilities/)</sup>

## 架构反模式

架构反模式关注系统内部结构与行为中的设计问题，从系统全局视角捕捉不良设计的成因与特征。与影响可理解性、可测试性等生命周期属性的架构坏味道不同，架构反模式可能对任何系统质量产生负面影响。<sup>[2](https://insights.sei.cmu.edu/blog/when-your-softwares-check-engine-light-is-on-identifying-design-problems-that-impact-software-failure/)</sup>

以下是常被引用的六种关键架构反模式，源自对可自动检测的设计原则违规的研究：<sup>[2](https://insights.sei.cmu.edu/blog/when-your-softwares-check-engine-light-is-on-identifying-design-problems-that-impact-software-failure/)</sup>

- **不稳定接口**（Unstable interface）：一个有众多依赖者的重要接口本应保持稳定，却变更频繁，迫使依赖文件反复修改，造成额外工作与更多故障点。
- **模块化违规组**（Modularity violation groups）：在修订历史中频繁一起变更、却并无结构依赖的模块，表明它们并不像设计的那样独立。
- **不健康的继承层次**（Unhealthy inheritance hierarchy）：父类依赖其子类，或客户端同时依赖父类与子类，破坏多态并传播缺陷。
- **交叉**（Crossing）：一个同时具有高扇入与高扇出、并常与其依赖者一同变更的文件，处于维护活动的中心，其复杂的相互依赖关系极易被破坏。
- **派系**（Clique）：由一条或多条依赖环绑定在一起的一组紧耦合文件。
- **包循环**（Package cycle）：包与包之间违反层次结构的依赖环，使一个包中的变更引起其他包中的意外变更。

## 有文献记载的目录

1998年，威廉·J·布朗（William J. Brown）、拉斐尔·C·马尔沃（Raphael C. Malveau）、海斯·麦考密克三世（Hays McCormick III）与托马斯·莫布雷（Thomas Mowbray）合著的《反模式：识别、重构与管理》一书描述了软件工程中出现的问题分类体系，并为识别问题和讨论解决方案提供了共同词汇，涵盖多个层面的措施：开发应用、设计软件系统以及管理软件项目。<sup>[3](https://adtmag.com/articles/2001/06/19/antipatterns.aspx)</sup> 其主要贡献是一份新的反模式目录。<sup>[7](https://ebooks.hgs.name/AntiPatterns%20-%20William%20J.%20Brown.pdf)</sup>

这套词汇并不局限于代码。在敏捷软件开发中，研究者研究了“ScrumBut”——即依赖Scrum的公司对推荐[Scrum](https://www.edgepedia.org/zh/scrum-software-development)实践的系统性偏离——并将这些偏离呈现为反模式，以推荐的教科书式实践作为替代的良好实践。<sup>[8](https://www.sciencedirect.com/science/article/pii/S0950584915002050)</sup> 谷歌的《谷歌软件工程》（*Software Engineering at Google*）一书在讨论工程管理时，同样将“Hire Pushovers”（招聘软柿子）之类的组织错误标为反模式。<sup>[9](https://raw.githubusercontent.com/abseil/abseil.github.io/cd13b21daa6ec74155548241241693198c1b1264/resources/swe_at_google.2.pdf)</sup>

## 影响的实证证据

由于反模式是关于设计的具名判断，研究者检验了它们是否可测量地损害软件工作。一项关于反模式普遍性与严重程度的大规模研究考察了1,717个用Java、C/C++和Python编写的开源项目，其动因是认识到：源代码中的次优结构——常被定义为坏味道或反模式——可能不会立即引入缺陷，但会随时间显著降低项目质量与生产力。<sup>[10](https://www.sciencedirect.com/science/article/pii/S095058492400034X)</sup>

另一项大规模实证研究考察了两种著名反模式——意大利面条式代码（Spaghetti Code）与巨块（Blob）——及其对程序理解的影响，其理论基础是：开发者通过形成关于领域的假设并将其映射到源代码来自上而下地理解程序。此前的研究曾报告，反模式显著影响维护期间编辑文件所需的开发者工作量。<sup>[4](https://arxiv.org/abs/2009.02438)</sup>

## 概念的运用

福勒就撰写和应用反模式提出了两点实用限定。第一，好的反模式描述最好能说明：如果已经走上那条坏路，该如何脱身——不过这属于有用而非必要；即便不存在好的出口，警告本身仍有价值。第二，同一解决方案在某些情境下可以是好模式，在另一些情境下则可能是反模式，因为解决方案的价值取决于其被使用的情境。<sup>[1](https://martinfowler.com/bliki/AntiPattern.html)</sup>

## References

1. [Martin Fowler, "Anti Pattern" (25 August 2015)](https://martinfowler.com/bliki/AntiPattern.html)
2. [SEI, "When Your Software's 'Check Engine' Light Is On: Identifying Design Problems that Impact Software Failure"](https://insights.sei.cmu.edu/blog/when-your-softwares-check-engine-light-is-on-identifying-design-problems-that-impact-software-failure/)
3. [ADTmag, "AntiPatterns" (2001)](https://adtmag.com/articles/2001/06/19/antipatterns.aspx)
4. [A Large Scale Empirical Study of the Impact of Spaghetti Code and Blob Anti-patterns on Program Comprehension, arXiv](https://arxiv.org/abs/2009.02438)
5. [SEI, "A Field Study of Technical Debt"](https://www.sei.cmu.edu/blog/a-field-study-of-technical-debt/)
6. [SEI, "Identifying the Architectural Roots of Vulnerabilities"](https://www.sei.cmu.edu/library/identifying-the-architectural-roots-of-vulnerabilities/)
7. [AntiPatterns: Identification, Refactoring, and Management (Brown et al.)](https://ebooks.hgs.name/AntiPatterns%20-%20William%20J.%20Brown.pdf)
8. [Exploring ScrumBut: An empirical study of Scrum anti-patterns, ScienceDirect](https://www.sciencedirect.com/science/article/pii/S0950584915002050)
9. [Software Engineering at Google](https://raw.githubusercontent.com/abseil/abseil.github.io/cd13b21daa6ec74155548241241693198c1b1264/resources/swe_at_google.2.pdf)
10. [Prevalence and severity of design anti-patterns in open source programs: A large-scale study, ScienceDirect](https://www.sciencedirect.com/science/article/pii/S095058492400034X)
11. [Antipatterns: Identification, refactoring, and management](https://pure.psu.edu/en/publications/antipatterns-identification-refactoring-and-management/)

---
*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/anti-pattern

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