内聚力和耦合力之间的差异 [英] Difference Between Cohesion and Coupling

查看:381
本文介绍了内聚力和耦合力之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the difference between cohesion and coupling?

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

How can coupling and cohesion lead to either good or poor software design?

哪些示例概述了两者之间的差异以及它们对整体代码质量的影响?

What are some examples that outline the difference between the two, and their impact on overall code quality?

推荐答案

内聚力是指类(或模块)可以做什么.缺乏凝聚力意味着班级会采取多种行动-范围很广,没有专注于应做的事情.高度的凝聚力意味着班级专注于应该做的事情,即仅与班级意图有关的方法.

Cohesion refers to what the class (or module) can do. Low cohesion would mean that the class does a great variety of actions - it is broad, unfocused on what it should do. High cohesion means that the class is focused on what it should be doing, i.e. only methods relating to the intention of the class.

低内聚力示例:

-------------------
| Staff           |
-------------------
| checkEmail()    |
| sendEmail()     |
| emailValidate() |
| PrintLetter()   |
-------------------

高内聚力示例:

----------------------------
| Staff                   |
----------------------------
| -salary                 |
| -emailAddr              |
----------------------------
| setSalary(newSalary)    |
| getSalary()             |
| setEmailAddr(newEmail)  |
| getEmailAddr()          |
----------------------------

关于耦合,是指两个类/模块彼此之间的相关性或依赖性.对于低耦合类,更改一个类中的主要内容不应影响另一类.高耦合将使更改和维护代码变得困难.由于各类之间的联系紧密,因此进行更改可能需要对整个系统进行改造.

As for coupling, it refers to how related or dependent two classes/modules are toward each other. For low coupled classes, changing something major in one class should not affect the other. High coupling would make it difficult to change and maintain your code; since classes are closely knit together, making a change could require an entire system revamp.

好的软件设计具有高内聚力低耦合.

这篇关于内聚力和耦合力之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆