耦合性和内聚性

发布于 2024-07-04 20:07:45 字数 302 浏览 6 评论 0原文

我试图将耦合和内聚的概念归结为一个简洁的定义。 有人能给我一个简短易懂的解释吗(比维基百科此处上的定义更短)和此处)? 他们如何互动?

谢谢。

有人有一个好的、简短的例子吗?

I'm trying to boil down the concepts of coupling and cohesion to a concise definition. Can someone give me a short and understandable explanation (shorter than the definitions on Wikipedia here and here)? How do they interact?

Thanks.

Anybody have a good, short example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷情 2024-07-11 20:07:45

测量耦合的一种快速而肮脏的方法是测量您的 import (或类似)语句。

A quick-and-dirty way to measure coupling is to measure your import (or similar) statements.

小伙你站住 2024-07-11 20:07:45

“耦合是模块之间相互依赖关系的衡量标准,
应该最小化”
“凝聚力是一种需要最大化的品质,注重关系
每个模块执行的活动之间。”

引用自本文:http://steve. vinoski.net/pdf/IEEE-Old_Measures_for_New_Services.pdf

"Coupling is a measure of interdependencies between modules,
which should be minimized"
"cohesion, a quality to be maximized, focuses on the relationships
between the activities performed by each module."

quoted from this paper: http://steve.vinoski.net/pdf/IEEE-Old_Measures_for_New_Services.pdf

北方的巷 2024-07-11 20:07:45

耦合

  • 松散:你和便利店里的那个人。 你们通过明确的协议进行沟通,以实现各自的目标——你付钱,他让你带着一袋奇多走出去。 你们中的任何一个都可以在不破坏系统的情况下被替换。

  • 紧:你和你的妻子。

凝聚力

  • 低:便利店。 从汽油到牛奶再到 ATM 银行业务,您都可以去那里购买各种东西。 产品和服务几乎没有共同点,将它们全部集中在一处的便利性可能不足以抵消由此导致的成本增加和质量下降。

  • 高:奶酪店。 他们卖奶酪。 没有其他的。 但就奶酪而言,无法击败他们。

Coupling

  • Loose: You and the guy at the convenience store. You communicate through a well-defined protocol to achieve your respective goals - you pay money, he lets you walk out with the bag of Cheetos. Either one of you can be replaced without disrupting the system.

  • Tight: You and your wife.

Cohesion

  • Low: The convenience store. You go there for everything from gas to milk to ATM banking. Products and services have little in common, and the convenience of having them all in one place may not be enough to offset the resulting increase in cost and decrease in quality.

  • High: The cheese store. They sell cheese. Nothing else. Can't beat 'em when it comes to cheese though.

或十年 2024-07-11 20:07:45

耦合 - 衡量一个模块(包、类、方法)对其他模块的依赖程度。 人们希望减少耦合,或者减少给定模块对系统其他模块的依赖程度。

内聚性 - 模块的成员(类、方法、方法内的功能)与同一模块的其他成员的相关程度的度量。 增加内聚性是可取的,因为这表明模块具有非常具体的任务并且只执行该任务。

Coupling - A measure of how much a module (package, class, method) relies on other modules. It is desirable to reduce coupling, or reduce the amount that a given module relies on the other modules of a system.

Cohesion - A measure of how closely related the members (classes, methods, functionality within a method) of a module are to the other members of the same module. It is desirable to increase cohesion as that indicates that a module has a very specific task and does only that task.

岁月静好 2024-07-11 20:07:45

耦合意味着对他人的依赖。
凝聚力意味着其自身的完整性。

Coupling means dependency on others.
Cohesion means completeness with itself.

香橙ぽ 2024-07-11 20:07:45

Bertrand Meyer 的 是与 OO 相关的软件设计概念(包括这些概念)最全面的讨论之一。面向对象的软件构建

关于“耦合”,他给出了他的弱耦合/小接口规则如下:

<块引用>

如果两个模块进行通信,它们应该交换尽可能少的信息。

Meyer 与内聚相关的材料从未被归结为一个简洁的陈述,但我认为这句话来自 Steve McConnell 的 代码完整总结得很好:

<块引用>

内聚性是指类中的所有例程或例程中的所有代码支持中心目的的紧密程度

One of the best comprehensive discussions of software design concepts related to OO (including these ones) is Bertrand Meyer's Object Oriented Software Construction.

Regarding 'coupling', he gives his Weak Coupling / Small Interfaces rule as follows:

If two modules communicate, they should exchange as little information as possible.

Meyer's material related to cohesion isn't ever boiled down to a single pithy statement, but I think this sentence from Steve McConnell's Code Complete sums it up pretty well:

Cohesion refers to how closely all the routines in a class or all the code in a routine support a central purpose

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文