# 整洁架构

**整洁架构**（Clean architecture）是由罗伯特·C·马丁（Robert C. Martin，人称“鲍勃大叔”）于2012年一篇博客文章中提出、并在其2017年著作《整洁架构》中系统化的一种软件架构风格。它将程序组织为同心圆层，其规则是源代码依赖只能向内指向业务规则，从而使系统核心独立于框架、用户界面、数据库和外部系统。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup><sup> • </sup><sup>[3](https://www.oreilly.com/library/view/clean-architecture-a/9780134494272/ch22.xhtml)</sup> 其宣称的目标是：独立于框架；无需UI、数据库或Web服务器即可测试；以及独立于UI、数据库和任何外部系统。<sup>[2](https://softengbook.org/articles/clean-architecture)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 罗伯特·C·马丁2012年的博客文章；在《整洁架构》（2017）第22章中展开<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup><sup> • </sup><sup>[3](https://www.oreilly.com/library/view/clean-architecture-a/9780134494272/ch22.xhtml)</sup> |
| 依赖规则 | 源代码依赖只向内指；内圈中的任何东西都不得知道外圈的任何东西，包括函数、类或变量的名称<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> |
| 四个环 | 实体（企业业务规则）、用例（应用特定规则）、接口适配器、框架与驱动器<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> |
| 五项宣称特性 | 独立于框架、无需UI/数据库/Web服务器即可测试、独立于UI、独立于数据库、独立于任何外部系统<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> |
| 实测收益 | 在一个重构后的后端中，可维护性指标提升21%至61%；Android准实验显示稳定性提升56%，可分析性提升7%<sup>[4](https://doi.org/10.1109/icoict55009.2022.9914890)</sup><sup> • </sup><sup>[5](https://revistas.itm.edu.co/index.php/tecnologicas/article/view/2104)</sup> |
| 实测成本 | 适配器、接口和映射器带来的样板代码增加；学习曲线陡峭；受访Android应用中仅23%保持严格的领域/基础设施分离<sup>[6](https://doi.org/10.55640/ijaair-v02i12-01)</sup><sup> • </sup><sup>[7](https://revistas.ufps.edu.co/index.php/respuestas/article/download/5627/6559/52240)</sup> |
| 模式家族 | 与六边形架构、端口与适配器架构、洋葱架构同属一个家族的相关模式，共享松耦合、依赖倒置的方法<sup>[8](https://github.com/ardalis/CleanArchitecture/)</sup> |

## 历史与谱系

马丁将整洁架构呈现为若干早期模式的整合，这些模式都将软件划分为层以分离关注点。他归功于阿利斯泰尔·科克伯恩（Alistair Cockburn）的[六边形架构](https://www.edgepedia.org/zh/hexagonal-architecture-software)（亦称端口与适配器），后者被史蒂夫·弗里曼（Steve Freeman）和纳特·普赖斯（Nat Pryce）采纳于《测试驱动的面向对象软件开发》；杰弗里·帕勒莫（Jeffrey Palermo）的洋葱架构；尖叫架构；詹姆斯·科普林（James Coplien）和特里格夫·雷恩斯考格（Trygve Reenskaug）的DCI（数据、上下文与交互）；以及伊瓦尔·雅各布森（Ivar Jacobson）的BCE。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup><sup> • </sup><sup>[3](https://www.oreilly.com/library/view/clean-architecture-a/9780134494272/ch22.xhtml)</sup> 2024年第二版《整洁代码》重复了同样的谱系。<sup>[9](https://www.oreilly.com/library/view/clean-code-a/9780135398586/ch27.xhtml)</sup>

该风格核心的实体/交互器/边界三元组可追溯至伊瓦尔·雅各布森的著作《面向对象软件工程：用例驱动方法》；马丁将用例描述为与交付无关的、划分系统的主要组织原则，而MVC和MVP则被限制在交付侧。<sup>[10](https://cleancoders.com/episode/clean-code-episode-7)</sup> 帕勒莫2008年的洋葱架构将应用拆分为同心层，领域模型居中，然后领域服务和应用服务构成应用核心，其规则是所有代码可以依赖更靠中心的层，但不能依赖更靠外的层。<sup>[11](https://www.planetgeek.ch/2024/09/19/onion-hexagonal-clean-or-fractal-architecture-all-of-them-and-more/)</sup> Thoughtworks将马丁2012年的版本描述为核心概念与洋葱架构相似、术语略有不同，领域模型变成了“实体”。<sup>[12](https://www.thoughtworks.com/en-us/insights/blog/architecture/demystify-software-architecture-patterns)</sup>

## 结构与依赖规则

该架构画出四个同心圆。中心是**实体**，即企业范围的业务规则。围绕它们，**用例**承载应用特定的业务规则。下一个环，**接口适配器**，在对用例和实体最方便的格式与对外部系统（如数据库或Web）最方便的格式之间转换数据；这个环包含MVC中的呈现器、视图和控制器。最外环，**框架与驱动器**，承载细节：数据库、Web框架、设备API。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup>

**依赖规则**规定源代码依赖只能向内指。内圈中的任何东西都不能对外圈中的任何东西有任何了解，甚至不知道其函数、类或变量的名称。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> 该规则管的是类型之间的静态依赖，而非控制流，控制流可以朝任一方向移动。<sup>[11](https://www.planetgeek.ch/2024/09/19/onion-hexagonal-clean-or-fractal-architecture-all-of-them-and-more/)</sup> 当控制流向外时，例如从用例到呈现器，边界通过依赖倒置原则跨越：用例调用一个接口，动态多态创造出与控流方向相反的源代码依赖，因此无论控制流向哪个方向，规则都成立。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> 红帽的实践指南将同一思想表述为传播规则：变更应只从内层移向外层，因此基础设施层的变更绝不应触发核心的变更。<sup>[13](https://developers.redhat.com/articles/2023/08/08/implementing-clean-architecture-solutions-practical-example)</sup>

四个圆是示意性的而非强制性的；允许更多层，但依赖规则始终适用，且抽象程度向中心递增。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> 整洁架构所细化的通用分层模式已知能促进复用性、可移植性、可维护性和可理解性，且“依赖只指向给定层的服务”这条规则执行得越严格，层间耦合就越少。<sup>[14](https://arxiv.org/pdf/2112.01644)</sup>

## 核心原则

马丁列出该风格旨在交付的五项特性：独立于框架；无需UI、数据库或Web服务器即可测试；独立于UI；独立于数据库；独立于任何外部系统。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> 他强调的实际回报是延迟决策：将团队与缓慢、消耗大量资源的IO设备和框架隔离开来的架构师，能让团队构建快速、轻量的测试环境，并让框架决策等到有足够信息时再做。<sup>[15](http://blog.cleancoder.com/uncle-bob/2016/01/04/ALittleArchitecture.html)</sup>

## 与其他架构的比较

该家族中的模式主要区别在于如何划分内部。六边形架构亦称端口与适配器；洋葱架构在领域模型周围添加同心环，但没有马丁明确的实体/用例划分；整洁架构命名了各环并增加了用例层。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup><sup> • </sup><sup>[11](https://www.planetgeek.ch/2024/09/19/onion-hexagonal-clean-or-fractal-architecture-all-of-them-and-more/)</sup><sup> • </sup><sup>[12](https://www.thoughtworks.com/en-us/insights/blog/architecture/demystify-software-architecture-patterns)</sup> 相对于分层MVC，接口适配器环是控制器、呈现器和视图所在之处，使它们远离业务核心。<sup>[1](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)</sup> 相对于领域驱动设计，埃尔贝托·格拉萨（Herberto Graça）2017年的“显式架构”等综合方案采纳了DDD的分层并将其纳入端口与适配器架构，以组织六边形内部的业务逻辑。<sup>[16](https://herbertograca.com/2017/11/16/explicit-architecture-01-ddd-hexagonal-onion-clean-cqrs-how-i-put-it-all-together/)</sup> 2024年的一项综合认为，实践中的团队会组合这些思想：带依赖方向规则的横向层、如六边形架构中的适配器、来自分形架构的递归嵌套，以及按功能分组代码的垂直切片。<sup>[11](https://www.planetgeek.ch/2024/09/19/onion-hexagonal-clean-or-fractal-architecture-all-of-them-and-more/)</sup>

## 数据说话

定量证据规模有限但方向一致。2022年一项IEEE研究将后端代码库重构为整洁架构，测量了圈复杂度、加权方法数、Kan缺陷、Halstead分数和可维护性指数；所有指标均改善，提升幅度从21%到61%不等。<sup>[4](https://doi.org/10.1109/icoict55009.2022.9914890)</sup> 一项Android准实验研究将以整洁架构和[ISO/IEC 25010](https://www.edgepedia.org/zh/iso-iec-25010)构建的Educar Teacher应用与对照应用比较；其基于普查的样本为693个包、2,037个类和168,217行代码，来自Educar Teacher；可维护性标准中的可分析性、稳定性、可测试性和可变更性分别提升7%、56%、0.7%和0.9%。<sup>[5](https://revistas.itm.edu.co/index.php/tecnologicas/article/view/2104)</sup>

一项对106名整洁架构用户的调查发现，其实施与整体软件质量之间存在显著正相关（r = 0.428，p < .001）。在各项架构实践中，依赖处理与质量（r = 0.707）和生产力（r = 0.691）的相关性最强，其次是关注点分离（质量r = 0.485，生产力r = 0.443）。<sup>[17](http://repo.lib.sab.ac.lk:8080/xmlui/handle/susl/5326)</sup> 更广泛地，一项对1,141个开源Java项目的研究计算了三个架构指标（每包类数、归一化距离和超额耦合度），发现未修复缺陷的数量显著取决于所有这些指标。<sup>[18](https://dl.acm.org/doi/10.1016/j.jss.2011.02.037)</sup> 关于忽视架构的代价，[巴里·博姆](https://www.edgepedia.org/zh/barry-boehm)（Barry Boehm）团队对25年间收集的161个项目的分析发现，在约10,000行代码的项目上对架构关注甚少，会在返工中耗费约14%的总项目时间，而在约1,000万行的项目上则升至约91%的额外返工时间。<sup>[19](https://frontendatscale.com/issues/13/)</sup>

## 采用与实践

具体映射展示了各环如何变成代码。微软的ISE团队将业务逻辑放在核心层，外部服务放在基础设施层，表示层作为入口点，依赖通过由依赖注入解析的接口向内流动；由于他们的领域严重依赖HL7 FHIR R4 NuGet包，他们将常见的四层（核心/领域、应用、基础设施、表示）合并为三层：核心、API和基础设施。<sup>[20](https://devblogs.microsoft.com/ise/next-level-clean-architecture-boilerplate/)</sup> 红帽将这一转变描述为用洋葱分层方法取代传统的千层面式面向服务方法（控制器、服务、数据库层），其核心承载领域类和用例，且不得依赖外部库。<sup>[13](https://developers.redhat.com/articles/2023/08/08/implementing-clean-architecture-solutions-practical-example)</sup> 一种Python表述使用四层：内层领域层包含核心业务对象，依赖极少；应用层编排“保存任务”或“完成任务”等工作流；薄接口层包含控制器；外层框架与驱动器层承载SQLAlchemy等易变依赖，领域层和应用层对上层一无所知。<sup>[21](https://deepengineering.net/p/pragmatic-clean-architecture-in-python)</sup>

**边界与横切关注点。** 在边缘，适配器是翻译器，而非业务逻辑之地：输入适配器将HTTP请求、消息和定时触发器转换为命令或查询，输出适配器实现核心定义的端口接口。DTO作为边界上的通货，使领域实体不外泄，但有一个例外：仓库是输出端口，合法地加载、持久化和重建领域聚合。Spring Data等持久化框架属于适配器内部，绝不在领域拥有的端口上，仓库的读侧（查找/读取）和写侧（保存/删除）方法可通过命名约定分离。边界跨越处的四种映射策略中，无映射是反模式，双向映射是务实的默认选择，完整桥接适合公共API、大型团队和长寿命系统。<sup>[22](https://www.manning.com/preview/clean-applications-with-hexagonal-architecture/chapter-5)</sup>

在Android上，对Now in Android和Plaid等参考仓库的分析表明，马丁同心模型的字面实现是例外；混合三模块配置（领域、数据、表示）占主导。<sup>[7](https://revistas.ufps.edu.co/index.php/respuestas/article/download/5627/6559/52240)</sup>

## 批评与权衡

有据可查的成本是具体的。将数据从一层传到另一层需要适配器模型、仓库接口和映射器，增加了额外样板代码并延长了初始搭建工作。<sup>[6](https://doi.org/10.55640/ijaair-v02i12-01)</sup> 实践者调查报告指出，学习曲线陡峭，尤其对入职期间的年轻工程师而言；工具和框架的约束也迫使开发者应对更大的复杂性。<sup>[17](http://repo.lib.sab.ac.lk:8080/xmlui/handle/susl/5326)</sup> Android仓库研究发现，在缺乏系统化重构的情况下，中等规模应用的领域层在密集开发的前六个月内往往会被技术细节污染，并列出过度架构是小规模或短寿命项目中反复出现的担忧；在具有多模块、分布式团队和两年以上视野的大型应用中，投资通过并行测试和在不影响业务逻辑的情况下替换技术而摊销。<sup>[7](https://revistas.ufps.edu.co/index.php/respuestas/article/download/5627/6559/52240)</sup> 微软的ISE团队就范围得出类似结论：整洁架构最适合必须长期保持可维护性和可扩展性的中大型复杂应用。<sup>[20](https://devblogs.microsoft.com/ise/next-level-clean-architecture-boilerplate/)</sup>

分层本身若不维护就会退化：随着系统演进，它们很少持续符合预期的分层模式，导致可维护性显著退化，这推动了检测分层违规的重构方法。<sup>[23](https://dl.acm.org/doi/10.1016/j.jss.2009.06.039)</sup>

## 主流框架与替代方案

该风格在主流材料中仍然流行。第二版《整洁代码》（2024）将整洁架构与其前身一同呈现。<sup>[9](https://www.oreilly.com/library/view/clean-code-a/9780135398586/ch27.xhtml)</sup> 一个面向[ASP.NET Core](https://www.edgepedia.org/zh/asp-net-core) 10的Pluralsight实验教授如何将现有应用重构为同心层，将黄金法则总结为依赖只向内指。<sup>[24](https://www.pluralsight.com/labs/codeLabs/guided-refactoring-an-aspnet-core-10-app-to-clean-architecture)</sup> 微软的On .NET节目邀请了社区MVP阿达利斯（Ardalis，即[史蒂夫·史密斯](https://www.edgepedia.org/zh/steve-smith)）讲解掌握整洁架构、实现垂直切片以获得更好的模块化，以及设计模块化单体。<sup>[25](https://learn.microsoft.com/en-us/shows/on-dotnet/on-dotnet-live-clean-architecture-vertical-slices-and-modular-monoliths-oh-my)</sup>

2023年后的主要竞争来自**垂直切片架构**和**模块化单体**。一篇对.NET方法的比较综述引用学术指标表明，从分层方案转向切片方案时，维护周期指数下降超过百分之二十，而代码内聚性上升；它还指出，引入异步交换和最终一致性的垂直切片/[CQRS](https://www.edgepedia.org/zh/cqrs)系统提高了入门门槛，并需要监控读写模型之间的失同步。<sup>[6](https://doi.org/10.55640/ijaair-v02i12-01)</sup> 在服务侧，红帽建议从基于整洁架构的单体开始，部署并测试它，只有在测试证明有利时才划分为迷你服务或微服务，或添加无服务器功能。<sup>[13](https://developers.redhat.com/articles/2023/08/08/implementing-clean-architecture-solutions-practical-example)</sup> 一位实践者的批评反驳说，对于细粒度微服务，如果实现一个功能需要跨十个微服务修改，那么在一个服务内实现会更容易。<sup>[26](https://threedots.tech/episode/is-clean-architecture-overengineering/)</sup>

## References

1. [Robert C. Martin (2012). The Clean Architecture. Clean Coder Blog.](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture)
2. [Building Software with a Clean Architecture. Software Engineering: A Modern Approach.](https://softengbook.org/articles/clean-architecture)
3. [Robert C. Martin (2017). Clean Architecture, Chapter 22. O'Reilly.](https://www.oreilly.com/library/view/clean-architecture-a/9780134494272/ch22.xhtml)
4. [Clean Architecture Implementation Impacts on Maintainability Aspect for Backend System Code Base (IEEE, 2022).](https://doi.org/10.1109/icoict55009.2022.9914890)
5. [Impact of Clean Architecture and ISO/IEC 25010 on the Maintainability of Android Applications. TecnoLógicas.](https://revistas.itm.edu.co/index.php/tecnologicas/article/view/2104)
6. [Comparative Review of Clean Architecture and Vertical Slice Architecture Approaches for Enterprise .NET Applications.](https://doi.org/10.55640/ijaair-v02i12-01)
7. [Empirical review of Clean Architecture adoption in Android repositories. Revista Respuestas.](https://revistas.ufps.edu.co/index.php/respuestas/article/download/5627/6559/52240)
8. [ardalis/CleanArchitecture. GitHub.](https://github.com/ardalis/CleanArchitecture/)
9. [Robert C. Martin (2024). Clean Code, 2nd Edition, Chapter 27. O'Reilly.](https://www.oreilly.com/library/view/clean-code-a/9780135398586/ch27.xhtml)
10. [Clean Code: Fundamentals, Episode 7. Clean Coders.](https://cleancoders.com/episode/clean-code-episode-7)
11. [Onion, Hexagonal, Clean or Fractal Architecture? All of them, and more!! planetgeek.ch (2024).](https://www.planetgeek.ch/2024/09/19/onion-hexagonal-clean-or-fractal-architecture-all-of-them-and-more/)
12. [Demystifying software architecture patterns. Thoughtworks.](https://www.thoughtworks.com/en-us/insights/blog/architecture/demystify-software-architecture-patterns)
13. [Implementing clean architecture solutions: A practical example. Red Hat Developer (2023).](https://developers.redhat.com/articles/2023/08/08/implementing-clean-architecture-solutions-practical-example)
14. [Systematically reviewing the layered architectural pattern principles and their use in reconstructing software architectures. arXiv.](https://arxiv.org/pdf/2112.01644)
15. [Robert C. Martin (2016). A Little Architecture. Clean Coder Blog.](http://blog.cleancoder.com/uncle-bob/2016/01/04/ALittleArchitecture.html)
16. [Herberto Graça (2017). DDD, Hexagonal, Onion, Clean, CQRS, … How I put it all together.](https://herbertograca.com/2017/11/16/explicit-architecture-01-ddd-hexagonal-onion-clean-cqrs-how-i-put-it-all-together/)
17. [An Empirical Evaluation of Clean Architecture: Impact on Software Quality and Developer Productivity.](http://repo.lib.sab.ac.lk:8080/xmlui/handle/susl/5326)
18. [An empirical study of software architectures' effect on product quality. Journal of Systems and Software.](https://dl.acm.org/doi/10.1016/j.jss.2011.02.037)
19. [Just Enough Architecture. Frontend at Scale, Issue 13.](https://frontendatscale.com/issues/13/)
20. [Next-Level Boilerplate: An Inside Look Into Our .Net Clean Architecture Repo. Microsoft ISE Developer Blog.](https://devblogs.microsoft.com/ise/next-level-clean-architecture-boilerplate/)
21. [Pragmatic Clean Architecture in Python: A Conversation with Sam Keen. Deep Engineering.](https://deepengineering.net/p/pragmatic-clean-architecture-in-python)
22. [Clean Applications with Hexagonal Architecture, Chapter 5. Manning early access.](https://www.manning.com/preview/clean-applications-with-hexagonal-architecture/chapter-5)
23. [Discovery of architectural layers and measurement of layering violations in source code. Journal of Systems and Software.](https://dl.acm.org/doi/10.1016/j.jss.2009.06.039)
24. [Guided: Refactoring an ASP.NET Core 10 App to Clean Architecture. Pluralsight.](https://www.pluralsight.com/labs/codeLabs/guided-refactoring-an-aspnet-core-10-app-to-clean-architecture)
25. [On .NET Live: Clean Architecture, Vertical Slices, and Modular Monoliths, Oh My. Microsoft Learn.](https://learn.microsoft.com/en-us/shows/on-dotnet/on-dotnet-live-clean-architecture-vertical-slices-and-modular-monoliths-oh-my)
26. [Is Clean Architecture Overengineering? Three Dots Labs podcast.](https://threedots.tech/episode/is-clean-architecture-overengineering/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › 软件工程与开发过程 › 软件设计与架构 › 标题4至Mo

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/clean-architecture

English original: https://www.edgepedia.org/clean-architecture
