耦合_wiki

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

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.

注:耦合和内聚相对,低耦合往往意味着高内聚,反之亦然。低耦合是良好的计算系统或设计的特征,同时,低耦合和高内聚可实现高可靠性和高可维护性的目标。

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.

注:用耦合和内聚来度量软件质量,最早在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:

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). 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.
依赖(操作&访问)其他模块的内部处理(含内部数据)
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 might imply 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 parts of it, possibly different parts (e.g., passing a whole record to a function that only needs one field of it).
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.
共享数据结构但可能使用不同成员。
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.
通过参数和消息来耦合。
No coupling
Modules do not communicate at all with one another.

以上分类不是很合理,个人理解的分类方式:

1、内容耦合:一个模块访问另一个模块的内部数据

2、公共耦合:两个模块共享全局数据

3、控制耦合:两个模块通过参数或消息交互,传递的数据可看到内部处理。控制耦合也意味着控制模块必须知道所控制模块内部的一些逻辑关系。

4、数据耦合:两个模块通过参数或消息交互,传递的数据经过抽象,和内部处理无关。

1和2一般程序可以避免,3和4可用于区分模块间耦合是否合理,因此模块功能和模块间的接口设计成为关键。

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.[3]

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.(由于一个模块有许多依赖的模块,导致模块的可复用性低)

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.

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

Solutions

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

注:重点是模块功能和接口设计,《functional design》词条并没有提供更多信息。

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

Coupling and cohesion are terms which occur together very frequently. Coupling refers to the interdependencies between modules, while cohesion describes how related the functions within a single module are. 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

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

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

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

时间: 2024-10-07 05:16:19

耦合_wiki的相关文章

关于“内聚和耦合”

内聚就是一个模块内各个元素彼此结合的紧密程度. 耦合就是一个软件结构内各个模块之间彼此结合的紧密程度. 内聚类型: 1.偶然内聚 模块的各成分之间没有关联,只是把分散的功能合并在一起. 2.逻辑内聚 逻辑上相关的功能被放在同一模块中. 3.时间内聚 模块完成的功能必须在同一时间内执行,但这些功能只是因为时间因素才有关联. 4.过程内聚 模块内部的处理成分是相关的,而且这些处理必须以特定的次序进行执行. 5.通信内聚 模块的所有成分都操作同一数据集或生成同一数据集. 6.顺序内聚 模块的各个成分和

iOS书写高质量代码之耦合的处理

原创 2016-12-26 MrPeak MrPeak杂货铺 耦合是每个程序员都必须面对的话题,也是容易被忽视的存在,怎么处理耦合关系到我们最后的代码质量.今天Peak君和大家聊聊耦合这个基本功话题,一起捋一捋iOS代码中处理耦合的种种方式及差异. 简化场景 耦合的话题可大可小,但原理都是相通的.为了方便讨论,我们先将场景进行抽象和简化,只讨论两个类之间的耦合. 假设我们有个类Person,需要喝水,根据职责划分,我们需要另一个类Cup来完成喝水的动作,代码如下: //Person.h @int

对高内聚,低耦合的理解

内聚:一个模块内各个元素彼此结合的紧密程度 耦合:一个软件结构内不同模块之间互连程度的度量 最近编码的时候,总是在犹豫是把某个方法封装在一个类里,还是单独的封装成一个类.这让我突然想起内聚耦合这两个名词. 我们一直追求着,高内聚,低耦合. 对于低耦合,我粗浅的理解是:一个完整的系统,模块与模块之间,尽可能的使其独立存在. 也就是说,让每个模块,尽可能的独立完成某个特定的子功能. 模块与模块之间的接口,尽量的少而简单. 如果某两个模块间的关系比较复杂的话,最好首先考虑进一步的模块划分. 这样有利于

电路的耦合方式

基本概念: 一级:组成多级放大电路的每一个基本放大电路称为一级. 级间耦合:级与级之间的连接称为级间耦合. 耦合电路往往与放大电路融为一体,不单独存在的. 多级放大电路的耦合方式:直接耦合.阻容耦合.变压器耦合和光电耦合. 直接耦合 直接耦合:将前一级的输出端直接连接到后一级的输入端. 如下图所示: 电路缺点:采用直接耦合方式使各级之间的直流通路相连,因而静态工作点相互影响.有零点漂移现象. 电路优点:具有良好的低频特性,可以放大变化缓慢的信号:由于电路中没有大容量电容,易于将全部电路集成在一片

浅谈高内聚低耦合

关键词:高内聚低耦合,网络消息,消息中间件 我所理解的高内聚是模块内部是独立完成某个单一的功能,尽可能的少而简单,也就是常说的单一责任原则.低耦合是各个模块之间相互独立存在,这样利于修改和组合.短期来看,并没有很明显的好处,甚至短期内会影响系统的开发进度,因为对开发设计人员提出了更高的要求,但长期来看,带来的好处是使程序更容易维护和修改. 在<由if-else,switch代替方案引起的思考>这篇文章里,有读者没太明白这种写法的好处(高内聚,低耦合), 当时没有展开来讲解.如果业务逻辑不复杂,

Effective前端5:减少前端代码耦合

什么是代码耦合?代码耦合的表现是改了一点毛发而牵动了全身,或者是想要改点东西,需要在一堆代码里面找半天.由于前端需要组织js/css/html,耦合的问题可能会更加明显,下面按照耦合的情况分别说明: 1. 避免全局耦合 这应该是比较常见的耦合.全局耦合就是几个类.模块共用了全局变量或者全局数据结构,特别是一个变量跨了几个文件.例如下面,在html里面定义了一个变量: 在html里面定义全局变量 XHTML 1 2 3 4 5 <script> varPAGE=20; </script&g

面向对象三大特性五大原则 + 低耦合高内聚

面向对象三大特性五大原则 + 低耦合高内聚 面向对象的三大特性是"封装."多态"."继承",五大原则是"单一职责原则"."开放封闭原则"."里氏替换原则"."依赖倒置原则"."接口分离原则". 什么是面向对象 面向对象(Object Oriented,OO)是软件开发方法.面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统.交互式界面.应用结

MEF实现设计上的“松耦合”(一)

1.什么是MEF 先来看msdn上面的解释:MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库. 应用程序开发人员可利用该库发现并使用扩展,而无需进行配置. 扩展开发人员还可以利用该库轻松地封装代码,避免生成脆弱的硬依赖项. 通过 MEF,不仅可以在应用程序内重用扩展,还可以在应用程序之间重用扩展. 也有人把MEF解释为“依赖注入”的一种方式,那么什么是“依赖注入”?如果这样解释,感觉越陷越深......根据博主的理解,了解MEF只需要

1.20 Java基础总结 输入数据类型判断 Java低耦合原则 for嵌套思路

一.方法后边都要有(),表示参数 二.需要输入Int型的,判断输入是否为IntScanner scan = new Scanner(System.in); if(scan.hasNextInt){ int a = scan.nextInt();}else{ System.out.println("输入无效:")} 三.Java设计原则:低耦合和单一原则 判断里边耦合度高(&&...&&..太多内容)多个变量可能出现问题,或者出现自己不想要的结果,也不利于