内聚和耦合之间的区别是什么?
耦合和内聚如何导致软件设计的好坏?
举些什么例子来概括这两者之间的区别,以及它们对整体代码质量的影响?
内聚和耦合之间的区别是什么?
耦合和内聚如何导致软件设计的好坏?
举些什么例子来概括这两者之间的区别,以及它们对整体代码质量的影响?
当前回答
简单地说,内聚性表示代码库的一部分在逻辑上形成单个原子单元的程度。另一方面,耦合表示单个单元对其他单元的依赖程度。换句话说,它是两个或多个单元之间的连接数。数量越少,耦合越低。
本质上,高内聚意味着将代码库中相互关联的部分保存在一个地方。同时,低耦合是关于尽可能多地分离代码库中不相关的部分。
从内聚和耦合角度来看的代码类型:
理想是遵循指导方针的代码。它是松散耦合和高度内聚的。我们可以用下图来说明这样的代码:
God Object是引入高内聚和高耦合的结果。它是一种反模式,基本上代表一段一次性完成所有工作的代码: 当不同类或模块之间的边界选择不当时,就会出现选择不当的情况
破坏性解耦是最有趣的一种。当程序员试图解耦代码库时,有时会发生这种情况,以至于代码完全失去了重点:
点击这里阅读更多
其他回答
凝聚力:Co的意思是在一起,hesion的意思是粘在一起。粘结:不同物质的微粒粘在一起的系统
举个现实的例子: img礼貌
整体大于部分之和-亚里士多德。
Cohesion is an ordinal type of measurement and is usually described as “high cohesion” or “low cohesion”. Modules with high cohesion tend to be preferable, because high cohesion is associated with several desirable traits of software including robustness, reliability, reusability, and understandability. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or even understand. wiki 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. wiki
提高内聚性和降低耦合确实会带来好的软件设计。
内聚对功能进行了划分,使其简洁并与相关数据最接近,而解耦则确保功能实现与系统的其余部分隔离。
解耦允许您在不影响软件其他部分的情况下更改实现。
内聚性确保实现更特定于功能,同时更容易维护。
减小耦合、提高内聚的最有效方法是采用界面设计。
也就是说,主要的功能对象只能通过它们实现的接口相互“认识”。接口的实现自然会引入内聚性。
虽然在某些场景中不太现实,但这应该是一个设计目标。
例子(非常粗略):
public interface IStackoverFlowQuestion
void SetAnswered(IUserProfile user);
void VoteUp(IUserProfile user);
void VoteDown(IUserProfile user);
}
public class NormalQuestion implements IStackoverflowQuestion {
protected Integer vote_ = new Integer(0);
protected IUserProfile user_ = null;
protected IUserProfile answered_ = null;
public void VoteUp(IUserProfile user) {
vote_++;
// code to ... add to user profile
}
public void VoteDown(IUserProfile user) {
decrement and update profile
}
public SetAnswered(IUserProfile answer) {
answered_ = answer
// update u
}
}
public class CommunityWikiQuestion implements IStackoverflowQuestion {
public void VoteUp(IUserProfile user) { // do not update profile }
public void VoteDown(IUserProfile user) { // do not update profile }
public void SetAnswered(IUserProfile user) { // do not update profile }
}
在你的代码库的其他地方,你可以有一个模块来处理问题,不管它们是什么:
public class OtherModuleProcessor {
public void Process(List<IStackoverflowQuestion> questions) {
... process each question.
}
}
关于内聚的最好解释来自Bob叔叔的《Clean Code》:
类应该有少量的实例变量。类的每个方法都应该操作一个或多个这样的变量。一般来说,方法操作的变量越多,该方法与其类的内聚性就越强。每个方法使用每个变量的类具有最大的内聚性。
一般来说,创建这种最大限度内聚的类既不可取,也不可能;另一方面,我们希望内聚力高。当内聚性高时,这意味着类的方法和变量是相互依赖的,并作为一个逻辑整体挂在一起。
保持函数小和参数列表短的策略有时会导致由方法子集使用的实例变量激增。当这种情况发生时,几乎总是意味着至少有一个其他类试图从较大的类中退出。您应该尝试将变量和方法分离到两个或多个类中,以便新类更具内聚性。
内聚性表明了软件元素的职责是如何相互关联和集中的。
耦合指的是软件元素与其他元素的连接强度。
软件元素可以是类、包、组件、子系统或系统。在设计系统时,建议使用具有高内聚性和支持低耦合的软件元素。
低内聚导致整体类难以维护、理解,降低了可重用性。类似地,高耦合导致类紧密耦合,更改往往不是非局部的,难以更改并减少重用。
我们可以假设一个场景,在这个场景中,我们正在设计一个典型的可监视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.
耦合=两个模块之间的交互/关系… 内聚=模块内两个元素之间的交互。
软件是由许多模块组成的。模块由元素组成。把一个模块看作一个程序。程序中的函数是一个元素。
在运行时,一个程序的输出被用作另一个程序的输入。这称为模块与模块之间的交互或流程与流程之间的通信。这也称为耦合。
在单个程序中,函数的输出被传递给另一个函数。这称为模块内元素的交互。这也被称为内聚。
例子:
耦合=两个不同家庭之间的交流…… 凝聚力=家庭中父亲、母亲和孩子之间的交流。