新闻详情

新闻详情

首页 / 资讯中心 / 详情

读书笔记:软件架构设计原则 - 组件原则(Principles of Component)(中英文对照)

发布时间:2026/9/29 7:40:41来源:尧图网络
读书笔记:软件架构设计原则 - 组件原则(Principles of Component)(中英文对照)
读【美】Robert C. Martin罗伯特 C. 马丁的《架构整洁之道》Clean Architecture有感做个整理。本文不是逐字翻译向算是个人见解的注释。组件内聚原则组件内聚原则主要讨论拿些类应该聚合在一个组件中以便组件既能提供相对完整的功能又不至于过于庞大。复用发布等同原则软件复用的最小粒度应该等同于其发布的最想粒度。也就是说如果你希望别以怎样的粒度复用你的软件你就应该以怎样的粒度发布你的软件。这其实就是组件的定义了组件是软件复用和发布的最小粒度软件单元。这个粒度既是复用粒度也是发布的粒度。共同封闭原则我们应该将那些会同时修改并且为了相同目的而修改的类放到同一个组件中。而将不会同时修改并且不会为了相同目的而修改的类放到不同的组件中。组件的目的虽然是为了复用然而开发中常常引发问题的恰恰在于组件本身的可维护性。如果组件在自己的生命周期中必须经历各种变更那么最好不要涉及其他组件相关的变更都在同一个组件中。这样当变更发生的时候只需要重新发布这个组件就可以了而不是一大堆组件都要受到牵连。共同复用原则不要强迫一个组件的用户依赖他们不需要的东西。这个原则一方面是说我们应该将相互依赖共同复用的类放在一个组件中。一个数据结构容器组件提供数组、Hash 表等各种数据结构容器那么对数据结构遍历的类、排序的类也应该放在这个组件中以使这个组件中的类共同对外提供服务。另一方面这个原则也说明如果不是被共同依赖的类就不应该放在同一个组件中。如果不被依赖的类发送变更就会引起组件变更进而引起使用组件的程序发生变更。这样就会导致组件的使用者产生不必要的困扰甚至讨厌使用这样的组件也造成了组件的复用的困难。原文参考Principles of Component CohesionIn the first group, Principles of Component Cohesion, you can find 3 principles:The Reuse/Release Equivalence Principle (REP)The granular of reuse is the granular of release.REP states that the granule of reuse, a component, can be no smaller than the granule of release. Anything that we reuse must also be released and tracked. It is not realistic for a developer to simply write a class and then claim that it is reusable. Reusability comes only after a tracking system is in place and offers the guarantees of notification, safety, and support that the potential reusers will need. REP gives us our first hint at how to partition our design into components. Since reusability must be based on components, reusable components must contain reusable classes. So, at least some components should comprise reusable sets of classes.Martin, Robert C. Agile Principles, Patterns, and Practices in C# . Pearson Education. Kindle Edition.The Common Reuse Principle (CRP)The classes in a component are reused together. If you reuse one of the classes in a component, you reuse them all.This principle helps us to decide which classes should be placed into a component. CRP states that classes that tend to be reused together belong in the same component. Classes are seldom reused in isolation. Generally, reusable classes collaborate with other classes that are part of the reusable abstraction. CRP states that these classes belong together in the same component. In such a component, we would expect to see classes that have lots of dependencies on each other. A simple example might be a container class and its associated iterators. These classes are reused together because they are tightly coupled. Thus, they ought to be in the same component.Martin, Robert C. Agile Principles, Patterns, and Practices in C# . Pearson Education. Kindle Edition.The Common Closure Principle (CCP)The classes in a component should be closed together against the same kinds of changes. A change that affects a component affects all the classes in that component and no other components.This is the Single-Responsibility Principle (SRP) restated for components. Just as SRP says that a class should not contain multiple reasons to change, CCP says that a component should not have multiple reasons to change. In most applications, maintainability is more important that reusability. If the code in an application must change, you would prefer the changes to occur all in one component rather than being distributed through many components. If changes are focused into a single component, we need redeploy only the one changed component. Other components that don’t depend on the changed component do not need to be revalidated or redeployed.Martin, Robert C. Agile Principles, Patterns, and Practices in C# . Pearson Education. Kindle Edition.来自 Principles of Package and Component Design组件耦合原则讨论组件之间的耦合关系应该如何设计。无循环依赖原则组件依赖关系不应该出现环。例如 组件 A 依赖 B、组件 B 依赖 C、组件 C 依赖 A形成循环依赖。文档依赖原则组件依赖关系必须指向更稳定的方向。较少变更的组件是稳定的也就是说经常变更的组件是不稳定的。不稳定的组件应该依赖稳定的组件。文档抽象原则一个组件的抽象化程度应该与其稳定性程度一致。一个稳定的组件应该是抽象的而不稳定的组件应该是具体的。如果你设计的组件具体、不稳定、那么可以为这个组件对外提供服务的类设计一组接口并把这组接口封装在一个专门的组件中那么这个组件相对就比较抽象、稳定。原文参考Principles of Component CouplingAnd for the second group, Principles of Component Coupling, we have another 3 principles.The Acyclic Dependencies Principle (ADP)Allow no cycles in the component dependency graph.The dependency structure must always be monitored for cycles. When cycles occur, they must be broken somehow. Sometimes, this will mean creating a new component, making the dependency structure grow.Martin, Robert C. Agile Principles, Patterns, and Practices in C# . Pearson Education. Kindle Edition.The Stable-Dependency Principle (SDP)Depend in the direction of stability.Designs cannot be completely static. Some volatility is necessary if the design is to be maintained. We accomplish this by conforming to CCP. Using this principle, we create components that are sensitive to certain kinds of changes. These components are designed to be volatile; we expect them to change. Any component that we expect to be volatile should not be depended on by a component that is difficult to change! Otherwise, the volatile component will also be difficult to change. It is the perversity of software that a module that you have designed to be easy to change can be made difficult to change by someone else simply hanging a dependency upon it. Not a line of source code in your module need change, and yet your module will suddenly be difficult to change. By conforming to SDP, we ensure that modules that are intended to be easy to change are not depended on by modules that are more difficult to change than they are.Martin, Robert C. Agile Principles, Patterns, and Practices in C# . Pearson Education. Kindle Edition.The Stable-Abstractions Principle (SAP)A component should be as abstract as it is stable.This principle sets up a relationship between stability and abstractness. It says that a stable component should also be abstract so that its stability does not prevent it from being extended. On the other hand, it says that an instable component should be concrete, since its instability allows the concrete code within it to be easily changed. Thus, if a component is to be stable, it should also consist of abstract classes so that it can be extended. Stable components that are extensibleMartin, Robert C. Agile Principles, Patterns, and Practices in C# . Pearson Education. Kindle Edition.来自 Principles of Package and Component Design
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

P4服务器部署核心原理:元数据与存储分离架构详解 2026/9/29 8:41:21

P4服务器部署核心原理:元数据与存储分离架构详解

1. 这不是“搭个服务器”那么简单:P4 本质是协同工作流的中枢神经系统如果你搜“P4服务器”,十有八九会看到一堆零散命令、配置片段,甚至误以为它和普通Linux服务一样——装个包、启个进程、开个端口就完事。我刚入行那会儿也这么想&#xff…

阅读更多 →
Coze二次开发实战:从低代码边界到私有化部署迁移路径 2026/9/29 8:41:21

Coze二次开发实战:从低代码边界到私有化部署迁移路径

说实话,看到“Coze二次开发”这个说法的时候,脑子里还停留在传统软件开发节奏的人,第一反应一定是“给我一份源码,我拿去改改再重新部署”。这个预期放在UG二次开发、金蝶二次开发、CATIA二次开发这些场景里没问题,但放…

阅读更多 →
Model-Optimizer不是工具,而是硬件约束下的模型优化方法论 2026/9/29 8:41:07

Model-Optimizer不是工具,而是硬件约束下的模型优化方法论

1. “Model-Optimizer”不是软件名,而是工程方法论的统称很多人第一次看到“Model-Optimizer”这个词,第一反应是——这是NVIDIA新出的某个GUI工具?是不是像NVIDIA Control Panel那样点几下就能让模型变快?我刚接手一个部署在RTX …

阅读更多 →
Springboot实现websocket通信 2026/9/29 8:40:41

Springboot实现websocket通信

目录 前言 一、websockt是什么? Websockt与 HTTP协议的核心区别 二、后端代码实现 三、前端代码实现 总结 前言 本文目标springboot实现websocket通信并且实现聊天功能。 一、websockt是什么? WebSocket 是一种在单个 TCP 连接上实现全双工通信的…

阅读更多 →
Jupytext 实战:如何处理 Markdown 格式中的“无效 YAML“原始单元格(Raw Cell)与元数据迁移 2026/9/29 8:40:41

Jupytext 实战:如何处理 Markdown 格式中的“无效 YAML“原始单元格(Raw Cell)与元数据迁移

开发工具 【免费下载链接】jupytext Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts 项目地址: https://gitcode.com/gh_mirrors/ju/jupytext 点击查看 免费下载 Jupytext 的核心能力之一,是把 Jupyter Notebook 以 Markdown、J…

阅读更多 →
PyCharm 配置 Git 全流程:从环境搭建到提交、分支与回滚避坑指南 2026/9/29 8:40:34

PyCharm 配置 Git 全流程:从环境搭建到提交、分支与回滚避坑指南

简介:这份PDF图文教程面向Python开发者与需要协同开发的团队成员,聚焦在PyCharm中配置并使用Git这一常见痛点,帮助读者在IDE内完成版本控制操作,免去频繁切换命令行的麻烦。资源包共1个PDF文件,大小约257KB&#xff0c…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉