<a href="https://en.wikipedia.org/wiki/Coupling_(computer_programming)" target="_blank">https://en.wikipedia.org/wiki/Coupling_(computer_programming)</a>
In software engineering, coupling is the degree of interdependence between software modules; a measure of how closely connected two routines or modules are;the strength of the relationships between modules.
注:在軟體工程中,耦合指子產品間的依賴程度,用于度量兩個例程(routines)或子產品間緊密程度。
Coupling is usually contrasted with cohesion. Low coupling often correlates with high cohesion, and vice versa. Low coupling is often a sign of a well-structured computer system and a good design, and when combined with high cohesion, supports the general goals of high readability and maintainability.
注:耦合和内聚相對,低耦合往往意味着高内聚,反之亦然。低耦合是良好的計算系統或設計的特征,同時,低耦合和高内聚可實作高可靠性和高可維護性的目标。
注:用耦合和内聚來度量軟體品質,最早在1960s有larry constantine在《結構化設計》中提及。
Coupling can be "low" (also "loose" and "weak") or "high" (also "tight" and "strong"). Some types of coupling, in order of highest to lowest coupling, are as follows:
A module here refers to a subroutine of any kind, i.e. a set of one or more statements having a name and preferably its own set of variable names.
<dl></dl>
<dt>1、Content coupling (high)</dt>
<dd>Content coupling (also known as Pathological coupling) occurs when one module modifies or relies on the internal workings of another module (e.g., accessing local data of another module). In this situation, a change in the way the second module produces data (location, type, timing) might also require a change in the dependent module.</dd>
<dd>依賴(操作&通路)其他子產品的内部處理(含内部資料)</dd>
<dt>2、Common coupling</dt>
<dd>Common coupling (also known as Global coupling) occurs when two modules share the same global data (e.g., a global variable). Changing the shared resource might imply changing all the modules using it.</dd>
<dd>共享全局資料(該資料不屬于這兩個子產品)</dd>
<dt>3、External coupling</dt>
<dd>External coupling occurs when two modules share an externally imposed data format, communication protocol, or device interface. This is basically related to the communication to external tools and devices.</dd>
<dd>共享外部資料格式/通信協定/裝置接口。</dd>
<dt>4、Control coupling</dt>
<dd>Control coupling is one module controlling the flow of another, by passing it information on what to do (e.g., passing a what-to-do flag).</dd>
<dd>控制另一個子產品的處理流程。</dd>
<dt>5、Stamp coupling (Data-structured coupling)</dt>
<dd>Stamp coupling occurs when modules share a composite data structure and use only parts of it, possibly different parts (e.g., passing a whole record to a function that only needs one field of it).</dd>
<dd>In this situation, a modification in a field that a module does not need may lead to changing the way the module reads the record.</dd>
<dd>共享資料結構但可能使用不同成員。</dd>
<dt>6、Data coupling</dt>
<dd>Data coupling occurs when modules share data through, for example, parameters. Each datum is an elementary piece, and these are the only data shared (e.g., passing an integer to a function that computes a square root).</dd>
<dd>資料傳遞</dd>
<dt>7、Message coupling (low)</dt>
<dd>通過參數和消息來耦合。</dd>
<dt>8、No coupling</dt>
<dd>Modules do not communicate at all with one another.</dd>
<dd></dd>
1、内容耦合:一個子產品通路另一個子產品的内部資料
2、公共耦合:兩個子產品共享全局資料
3、控制耦合:兩個子產品通過參數或消息互動,傳遞的資料可看到内部處理。控制耦合也意味着控制子產品必須知道所控制子產品内部的一些邏輯關系。
4、資料耦合:兩個子產品通過參數或消息互動,傳遞的資料經過抽象,和内部處理無關。
1和2一般程式可以避免,3和4可用于區分子產品間耦合是否合理(3合理,4不合理),是以子產品功能和子產品間的接口設計成為關鍵。
<dt>Subclass coupling</dt>
<dd>Describes the relationship between a child and its parent. The child is connected to its parent, but the parent is not connected to the child.</dd>
<dt>Temporal coupling</dt>
<dd>When two actions are bundled together into one module just because they happen to occur at the same time.</dd>
Tightly coupled systems tend to exhibit the following developmental characteristics, which are often seen as disadvantages:
Assembly of modules might require more effort and/or time due to the increased inter-module dependency.(由于子產品之間的互相依賴,子產品的組合會需要更多的精力和時間)
A particular module might be harder to reuse and/or test because dependent modules must be included.(由于一個子產品有許多依賴的子產品,導緻子產品的可複用性低)
<dt>Message Transmission Overhead and Performance</dt>
<dd>Since a message must be transmitted in full to retain its complete meaning, message transmission must be optimized. Longer messages require more CPU and memory to transmit and receive. Also, when necessary, receivers must reassemble a message into its original state to completely receive it. Hence, to optimize runtime performance, message length must be minimized and message meaning must be maximized.</dd>
<dt>Message Translation Overhead and Performance</dt>
<dd>Message protocols and messages themselves often contain extra information (i.e., packet, structure, definition and language information). Hence, the receiver often needs to translate a message into a more refined form by removing extra characters and structure information and/or by converting values from one type to another. Any sort of translation increases CPU and/or memory overhead. To optimize runtime performance, message form and content must be reduced and refined to maximize its meaning and reduce translation.</dd>
<dt>Message Interpretation Overhead and Performance</dt>
A has an attribute that refers to (is of type) B.
A calls on services of an object B.
A has a method that references B (via return type or parameter).
A is a subclass of (or implements) class B.
Coupling in Software Engineering describes a version of metrics associated with this concept.
For data and control flow coupling:
di: number of input data parameters
ci: number of input control parameters
do: number of output data parameters
co: number of output control parameters
For global coupling:
gd: number of global variables used as data
gc: number of global variables used as control
For environmental coupling:
w: number of modules called (fan-out)
r: number of modules calling the module under consideration (fan-in)
{\displaystyle \mathrm {Coupling} (C)=1-{\frac {1}{d_{i}+2\times c_{i}+d_{o}+2\times c_{o}+g_{d}+2\times g_{c}+w+r}}}
<code>Coupling(C)</code> makes the value larger the more coupled the module is. This number ranges from approximately 0.67 (low coupling) to 1.0 (highly coupled)
For example, if a module has only a single input and output data parameter
{\displaystyle C=1-{\frac {1}{1+0+1+0+0+0+1+0}}=1-{\frac {1}{3}}=0.67}
If a module has 5 input and output data parameters, an equal number of control parameters, and accesses 10 items of global data, with a fan-in of 3 and a fan-out of 4,
{\displaystyle C=1-{\frac {1}{5+2\times 5+5+2\times 5+10+0+3+4}}=0.98}