耦合

https://en.wikipedia.org/wiki/Coupling_(computer_programming)


In software engineeringcoupling is the manner and degree of interdependence between software modules; a measure of how closely connected two routines or modules are; the strength of the relationships between modules.

在软件工程领域,耦合性是描述软件模块间相互依赖的行为和程度;是两个路径或模块间紧密程度的一种衡量方式;是模块间关系强度的一种度量。

Coupling is usually contrasted with cohesionLow 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.

耦合性通常用来与内聚性作对比。低耦合常常和高内聚联系在一起,反之亦然。低耦合经常被用来作为精良电脑系统结构和优秀设计的一种标志。如果同时又具备高内聚,那么它就达到了高可读性和高维护性的总体目标。


History(历史)

The software quality metrics of coupling and cohesion were invented by Larry Constantine in the late 1960s as part of Structured Design, based on characteristics of “good” programming practices that reduced maintenance and modification costs. Structured Design, including cohesion and coupling, were published in the article Stevens, Myers & Constantine (1974) and the book Yourdon & Constantine (1979), and the latter subsequently became standard terms.

在1960年代末, Larry Constantine发明了一种软件质量的度量方式:耦合性和内聚性。它是作为结构化设计的一部分。结构化设计的宗旨在于优秀的程序实践,降低维护和修改成本。结构化设计(包括内聚性和耦合性),在文献 Stevens, Myers & Constantine (1974 )和著作Yourdon & Constantine (1979)中问世,而且很快就成为标准。

Types of coupling(耦合类型)

Conceptual model of coupling

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:

耦合性可描述为“低”(也可以是“松”和“弱”)或“高”(也可以是“紧”和“强”)。耦合性从高到低排列如下:

Procedural programming(程序化编程)

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.

本文中的模块指的是任何单元的子程序,例如:有一条或多条语句的程序,有名称并且最好有自己的的一组变量。

Content coupling (high)(内容耦合(高))
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).
Therefore changing the way the second module produces data (location, type, timing) will lead to changing the dependent module.
内容耦合(也被称为病理耦合)发生在:当一个模块修改或依赖于另一个模块的内部实现(例如,访问另一个模块的局部数据)
因此,一个模块数据(位置,类型,计时)的变动会引起依赖模块的变动。
Common coupling(一般耦合)
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 implies changing all the modules using it.
一般耦合( 也被称为全局耦合)发生在:两个模块共享相同的全局数据(例如全局变量)。
共享资源的变动意味着所有使用它的模块都需要相应调整。
External coupling(外部耦合)

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.

外部耦合发生在:两个模块共享外部数据形式、通讯协议或者设备接口。

它主要和通讯使用的外部工具和设备相关。

Control coupling(控制耦合)

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).

控制耦合指的是一个模块通过传递做什么的信息(例如传递做什么的标示)来控制另一个模块的流程。

Stamp coupling (Data-structured coupling)( 标记耦合
Stamp coupling occurs when modules share a composite data structure and use only a part of it, possibly a different part (e.g., passing a whole record to a function that only needs one field of it).

This may lead to changing the way a module reads a record because a field that the module does not need has been modified.

标记耦合发生在:多个模块共享一个数据结构的综合体,但是每个模块只使用其关注的一部分--可能都是不同的部分(例如一个函数的摸个参数是整个记录,但实际上只需要其中一个字段)。

这可能会导致一个模块必须改变读记录的方式,因为模块不需要的字段的值很可能已经被修改了。

Data coupling(数据耦合)

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).

数据耦合发生在:模块共享传输的数据,例如,参数。每个数据都是一个单元片段,并且只有他们是数据共享的(例如传递一个整形给一个求平方根的函数)

Message coupling (low)(消息耦合)

This is the loosest type of coupling. It can be achieved by state decentralization (as in objects) and component communication is done via parameters or message passing (see Message passing).

这是耦合性最低的类型。它可以通过参数或消息传递实现各状态(如对象)和组件通讯。

No coupling(无耦合)

Modules do not communicate at all with one another.

模块之间没有任何通讯。

Object-oriented programming(面向对象编程)

Subclass Coupling(子类耦合)

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.

用来描述子类和父类之间的联系。子类要联系父类,但是父类却不会联系子类。

Temporal coupling(临时耦合)
When two actions are bundled together into one module just because they happen to occur at the same time.

In recent work various other coupling concepts have been investigated and used as indicators for different modularization principles used in practice.

  当两个动作被绑在一起给一个模块,仅仅因为在同一时刻发生

近期各种其它耦合概念被研究和使用,用来作为实践中不同模块化规则的依据。

Disadvantages(缺点)

Tightly coupled systems tend to exhibit the following developmental characteristics, which are often seen as disadvantages:

  1. A change in one module usually forces a ripple effect of changes in other modules.
  2. Assembly of modules might require more effort and/or time due to the increased inter-module dependency.
  3. A particular module might be harder to reuse and/or test because dependent modules must be included.
深度耦合的系统在 开发过程中表现出以下特征,而这些特征常常被认作是缺点:

  1. 一个模块的变动常常发生连锁反应,强制其它模块跟着变动.
  2. 模块整合因为交互模块的依赖性,可能需要更多的精力和(或)时间.
  3. 特殊模块可能很难复用和(或测试),因为其依赖的的模块必须被引用.

Performance issues[性能问题]

Whether loosely or tightly coupled, a system's performance is often reduced by message and parameter creation, transmission, translation (e.g. marshaling) and message interpretation (which might be a reference to a string, array or data structure), which require less overhead than creating a complicated message such as a SOAP message. Longer messages require more CPU and memory to produce. To optimize runtime performance, message length must be minimized and message meaning must be maximized.

不管是松耦合还是紧耦合,系统性能通常因为以下因素而下降:消息和参数的创建、传输、转译(如信号编集)和消息解析(可能是一个字符串、一个数组或者是一个数据结构的引用),相对而言,这些开销比起创建一个复杂的消息(如SOAP消息)小。执行长消息需要占有更长的CPU和更多的内存。为了优化性能,消息长度必须最小化,同时容纳的信息最大化。

Message Transmission Overhead and Performance(消息传输的开销和性能)

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.

因为为了保证其完整意义,消息必须被全部传输,所以消息传输必须优化。传输和接收长消息需要占有更长的CPU和更多的内存。另外如果需要,接收者必须按照原有状态重新整理消息,这样才算完成接收。因此为了优化实时性能,消息长度必须最小化,同时容纳的信息最大化。

Message Translation Overhead and Performance(消息转译的开销和性能)

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.

消息协议和消息自身通常包含额外信息(如:包、结构、定义和语言信息)。因此,接收者常常需要通过去除额外字符和结构信息,和/或者把值从一种类型转换成另外一种,以达到把消息转译成更加提炼的形态。任何一种转译都会增加CPU和/或者内存开销。为了优化实时性能,消息形式和内容必须减少和提炼,最大化意义和减少转译。

Message Interpretation Overhead and Performance(消息解析的开销和性能)

All messages must be interpreted by the receiver. Simple messages such as integers might not require additional processing to be interpreted. However, complex messages such as SOAP messages require a parser and a string transformer for them to exhibit intended meanings. To optimize runtime performance, messages must be refined and reduced to minimize interpretation overhead.

所有消息必须能够被接收者明确。简单消息如整数可以不需要额外的解释。但是,复杂消息如SOAP消息需要解释器和字符串转换器,用来转成想要的意思。为了优化实时性能,消息必须要提炼和减少,最小化解析开销。

Solutions[edit]

One approach to decreasing coupling is functional design, which seeks to limit the responsibilities of modules along functionality, coupling increases between two classes A and B if:

  • 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.

Low coupling refers to a relationship in which one module interacts with another module through a simple and stable interface and does not need to be concerned with the other module's internal implementation (see Information Hiding).

Systems such as CORBA or COM allow objects to communicate with each other without having to know anything about the other object's implementation. Both of these systems even allow for objects to communicate with objects written in other languages.

Coupling versus cohesion[edit]

Coupling and cohesion are terms which occur together very frequently. Coupling refers to the interdependencies between modules, while cohesion describes how related are the functions within a single module. Low cohesion implies that a given module performs tasks which are not very related to each other and hence can create problems as the module becomes large.

Module coupling[edit]

Coupling in Software Engineering[4] 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)

\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}

Coupling(C) 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

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,

C = 1 - \frac{1}{5 + 2\times 5 + 5 + 2\times 5 + 10 + 0 + 3 + 4} = 0.98



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值