内聚和耦合之间的区别是什么?
耦合和内聚如何导致软件设计的好坏?
举些什么例子来概括这两者之间的区别,以及它们对整体代码质量的影响?
内聚和耦合之间的区别是什么?
耦合和内聚如何导致软件设计的好坏?
举些什么例子来概括这两者之间的区别,以及它们对整体代码质量的影响?
当前回答
提高内聚性和降低耦合确实会带来好的软件设计。
内聚对功能进行了划分,使其简洁并与相关数据最接近,而解耦则确保功能实现与系统的其余部分隔离。
解耦允许您在不影响软件其他部分的情况下更改实现。
内聚性确保实现更特定于功能,同时更容易维护。
减小耦合、提高内聚的最有效方法是采用界面设计。
也就是说,主要的功能对象只能通过它们实现的接口相互“认识”。接口的实现自然会引入内聚性。
虽然在某些场景中不太现实,但这应该是一个设计目标。
例子(非常粗略):
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.
}
}
其他回答
提高内聚性和降低耦合确实会带来好的软件设计。
内聚对功能进行了划分,使其简洁并与相关数据最接近,而解耦则确保功能实现与系统的其余部分隔离。
解耦允许您在不影响软件其他部分的情况下更改实现。
内聚性确保实现更特定于功能,同时更容易维护。
减小耦合、提高内聚的最有效方法是采用界面设计。
也就是说,主要的功能对象只能通过它们实现的接口相互“认识”。接口的实现自然会引入内聚性。
虽然在某些场景中不太现实,但这应该是一个设计目标。
例子(非常粗略):
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.
}
}
简单地说,内聚意味着一个类应该代表一个单一的概念。
如果类的所有特性都与类所代表的概念相关,那么类的公共接口就是内聚的。 例如,与其拥有CashRegister类,不如拥有CashRegister和Coin功能,将其整合为2个类- CashRegister和Coin类。
在耦合中,一个类依赖于另一个类,因为它使用类的对象。
高耦合的问题在于它会产生副作用。一个类中的一个更改可能导致另一个类中出现意外错误,并可能破坏整个代码。
通常,高内聚和低耦合被认为是高质量的OOP。
我认为区别可以归结为以下几点:
内聚表示代码库的一部分在逻辑上形成单一原子单元的程度。 耦合表示单个单元独立于其他单元的程度。 在不破坏内聚的情况下存档完全解耦是不可能的,反之亦然。
在这篇博文中,我将对此进行更详细的描述。
耦合=两个模块之间的交互/关系… 内聚=模块内两个元素之间的交互。
软件是由许多模块组成的。模块由元素组成。把一个模块看作一个程序。程序中的函数是一个元素。
在运行时,一个程序的输出被用作另一个程序的输入。这称为模块与模块之间的交互或流程与流程之间的通信。这也称为耦合。
在单个程序中,函数的输出被传递给另一个函数。这称为模块内元素的交互。这也被称为内聚。
例子:
耦合=两个不同家庭之间的交流…… 凝聚力=家庭中父亲、母亲和孩子之间的交流。
理论的区别
凝聚力
内聚性是模块相对功能强度的表征。 内聚模块执行单一任务,几乎不需要与其他模块交互 程序其他部分的组件。 具有高内聚和低耦合的模块称为功能独立模块 其他模块。
衔接的分类
1.巧合2。逻辑3。时间4。程序5。沟通6。连续7。功能
耦合
耦合表示模块之间的相对依赖关系。 两个模块之间的耦合程度取决于它们的接口复杂性。