https://en.wikipedia.org/wiki/Coupling_(computer_programming)
In software engineering, coupling 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 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.
耦合性通常用来与内聚性作对比。低耦合常常和高内聚联系在一起,反之亦然。低耦合经常被用来作为精良电脑系统结构和优秀设计的一种标志。如果同时又具备高内聚,那么它就达到了高可读性和高维护性的总体目标。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)中问世,而且很快就成为标准。
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.
本文中的模块指的是任何单元的子程序,例如:有一条或多条语句的程序,有名称并且最好有自己的的一组变量。
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.
模块之间没有任何通讯。
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.
用来描述子类和父类之间的联系。子类要联系父类,但是父类却不会联系子类。
In recent work various other coupling concepts have been investigated and used as indicators for different modularization principles used in practice.
当两个动作被绑在一起给一个模块,仅仅因为在同一时刻发生。
近期各种其它耦合概念被研究和使用,用来作为实践中不同模块化规则的依据。
Tightly coupled systems tend to exhibit the following developmental characteristics, which are often seen as disadvantages:
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 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和/或者内存开销。为了优化实时性能,消息形式和内容必须减少和提炼,最大化意义和减少转译。
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消息需要解释器和字符串转换器,用来转成想要的意思。为了优化实时性能,消息必须要提炼和减少,最小化解析开销。
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:
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 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.
Coupling in Software Engineering[4] describes a version of metrics associated with this concept.
For data and control flow coupling:
For global coupling:
For environmental coupling:
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
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,