耦合與內聚 (Coupling and cohesion)


問題描述

耦合和內聚 (Coupling and cohesion)

我試圖將耦合和內聚的概念歸結為一個簡潔的定義。誰能給我一個簡短易懂的解釋(比維基百科上的定義短這裡這裡)?它們是如何交互的?

謝謝。

有人有一個很好的簡短例子嗎?


參考解法

方法 1:

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.

方法 2:

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.

方法 3:

Coupling means dependency on others.

Cohesion means completeness with itself.</p>

方法 4:

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

方法 5:

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

(by JonathanShog9Thomas OwensMansoor MehmoodMcKenzieG1Hank Gay)

參考文件

  1. Coupling and cohesion (CC BY‑SA 2.5/3.0/4.0)

#ooad #architecture #theory #OOP






相關問題

接口實現是否應該獨立 (Should Interface implementations be independent)

當我們有不同的回報類型時實現策略模式 (Achieve strategy pattern when we have different return type)

抽象VS信息隱藏VS封裝 (Abstraction VS Information Hiding VS Encapsulation)

應用程序中jQuery表單綁定代碼的最佳實踐 (Best practices with jQuery form binding code in an application)

耦合與內聚 (Coupling and cohesion)

什麼時候應該在C ++中使用類與結構? (When should you use a class vs a struct in C++?)

What is an anti-pattern? (What is an anti-pattern?)

如何根據編程代碼顯示聚合? (How to Show Aggregation in terms of A Programming Code?)

協調 MVP 三元組 (Coordinating MVP triads)

裝飾器和虛擬方法 (Decorators and Virtual Methods)

設計和建築有什麼區別? (What is the difference between Design and Architecture?)

特定時間段有效的業務規則——如何有序管理 (Business rules that are valid for specific time span – how to manage in an orderly manner)







留言討論