尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

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

发布时间:2026/9/29 7:40:38

资讯中心
01
ARTICLE

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

读书笔记:软件架构设计原则 - 组件原则(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
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

◈

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

◐

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

▲

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。