内聚和耦合的区别 [英] Difference Between Cohesion and Coupling

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

问题描述

内聚和耦合有什么区别?

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天全站免登陆