内聚和耦合之间的区别是什么?

耦合和内聚如何导致软件设计的好坏?

举些什么例子来概括这两者之间的区别,以及它们对整体代码质量的影响?


当前回答

内聚性表明了软件元素的职责是如何相互关联和集中的。

耦合指的是软件元素与其他元素的连接强度。

软件元素可以是类、包、组件、子系统或系统。在设计系统时,建议使用具有高内聚性和支持低耦合的软件元素。

低内聚导致整体类难以维护、理解,降低了可重用性。类似地,高耦合导致类紧密耦合,更改往往不是非局部的,难以更改并减少重用。

我们可以假设一个场景,在这个场景中,我们正在设计一个典型的可监视ConnectionPool,并满足以下需求。注意,对于像ConnectionPool这样的简单类来说,它可能看起来太过了,但基本目的只是用一些简单的示例演示低耦合和高内聚,我认为应该会有所帮助。

支持连接 释放连接 获取有关连接与使用计数的统计数据 获取有关连接和时间的统计数据 将连接检索和发布信息存储到数据库中,以便以后报告。

对于低内聚,我们可以通过将所有功能/职责强制填充到单个类中来设计ConnectionPool类,如下所示。我们可以看到,这个类负责连接管理、与数据库交互以及维护连接统计信息。

有了高内聚性,我们可以在类之间分配这些职责,并使其更可维护和可重用。

To demonstrate Low coupling we will continue with the high cohesion ConnectionPool diagram above. If we look at the above diagram although it supports high cohesion, the ConnectionPool is tightly coupled with ConnectionStatistics class and PersistentStore it interacts with them directly. Instead to reduce the coupling we could introduce a ConnectionListener interface and let these two classes implement the interface and let them register with ConnectionPool class. And the ConnectionPool will iterate through these listeners and notify them of connection get and release events and allows less coupling.

Note/Word or Caution: For this simple scenario it may look like an overkill but if we imagine a real-time scenario where our application needs to interact with multiple third party services to complete a transaction: Directly coupling our code with the third party services would mean that any changes in the third party service could result in changes to our code at multiple places, instead we could have Facade that interacts with these multiple services internally and any changes to the services become local to the Facade and enforce low coupling with the third party services.

其他回答

耦合=两个模块之间的交互/关系… 内聚=模块内两个元素之间的交互。

软件是由许多模块组成的。模块由元素组成。把一个模块看作一个程序。程序中的函数是一个元素。

在运行时,一个程序的输出被用作另一个程序的输入。这称为模块与模块之间的交互或流程与流程之间的通信。这也称为耦合。

在单个程序中,函数的输出被传递给另一个函数。这称为模块内元素的交互。这也被称为内聚。

例子:

耦合=两个不同家庭之间的交流…… 凝聚力=家庭中父亲、母亲和孩子之间的交流。

理论的区别

凝聚力

内聚性是模块相对功能强度的表征。 内聚模块执行单一任务,几乎不需要与其他模块交互 程序其他部分的组件。 具有高内聚和低耦合的模块称为功能独立模块 其他模块。

衔接的分类

1.巧合2。逻辑3。时间4。程序5。沟通6。连续7。功能

耦合

耦合表示模块之间的相对依赖关系。 两个模块之间的耦合程度取决于它们的接口复杂性。

内聚性表明了软件元素的职责是如何相互关联和集中的。

耦合指的是软件元素与其他元素的连接强度。

软件元素可以是类、包、组件、子系统或系统。在设计系统时,建议使用具有高内聚性和支持低耦合的软件元素。

低内聚导致整体类难以维护、理解,降低了可重用性。类似地,高耦合导致类紧密耦合,更改往往不是非局部的,难以更改并减少重用。

我们可以假设一个场景,在这个场景中,我们正在设计一个典型的可监视ConnectionPool,并满足以下需求。注意,对于像ConnectionPool这样的简单类来说,它可能看起来太过了,但基本目的只是用一些简单的示例演示低耦合和高内聚,我认为应该会有所帮助。

支持连接 释放连接 获取有关连接与使用计数的统计数据 获取有关连接和时间的统计数据 将连接检索和发布信息存储到数据库中,以便以后报告。

对于低内聚,我们可以通过将所有功能/职责强制填充到单个类中来设计ConnectionPool类,如下所示。我们可以看到,这个类负责连接管理、与数据库交互以及维护连接统计信息。

有了高内聚性,我们可以在类之间分配这些职责,并使其更可维护和可重用。

To demonstrate Low coupling we will continue with the high cohesion ConnectionPool diagram above. If we look at the above diagram although it supports high cohesion, the ConnectionPool is tightly coupled with ConnectionStatistics class and PersistentStore it interacts with them directly. Instead to reduce the coupling we could introduce a ConnectionListener interface and let these two classes implement the interface and let them register with ConnectionPool class. And the ConnectionPool will iterate through these listeners and notify them of connection get and release events and allows less coupling.

Note/Word or Caution: For this simple scenario it may look like an overkill but if we imagine a real-time scenario where our application needs to interact with multiple third party services to complete a transaction: Directly coupling our code with the third party services would mean that any changes in the third party service could result in changes to our code at multiple places, instead we could have Facade that interacts with these multiple services internally and any changes to the services become local to the Facade and enforce low coupling with the third party services.

内聚性是一个模块相对功能强度的指示。

A cohesive module performs a single task, requiring little interaction with other components in other parts of a program. Stated simply, a cohesive module should (ideally) do just one thing. Conventional view: the “single-mindedness” of a module OO view: cohesion implies that a component or class encapsulates only attributes and operations that are closely related to one another and to the class or component itself Levels of cohesion Functional Layer Communicational Sequential Procedural Temporal utility

耦合表示模块之间的相对相互依赖。

耦合取决于模块之间的接口复杂性 指向一个模块的条目或引用,以及什么数据 通过接口传递。 传统观点: 一个组件与其他组件和外部世界的连接程度 OO视图:类之间连接程度的定性度量 耦合水平 内容 常见 控制 邮票 数据 程序调用 类型使用 包含或导入 外部#

我认为区别可以归结为以下几点:

内聚表示代码库的一部分在逻辑上形成单一原子单元的程度。 耦合表示单个单元独立于其他单元的程度。 在不破坏内聚的情况下存档完全解耦是不可能的,反之亦然。

在这篇博文中,我将对此进行更详细的描述。