0

我将尝试写一些关于软件耦合和凝聚力的想法,但我不确定它们是否意味着任何实际的东西。所以如果你想用例子来解释你的答案,请使用简单的代数表达式,想象代数是一种顺序编程语言,这样我们都可以理解你在说什么......

在维基百科中阅读它

所以这就是我想要相信的(这是正确的吗?):

'Implementation of A with Low Cohesion
'(Coincidental cohesion because there is no 
' good reason or need to group the functions
' in this way)
a(x) = 2x + 1
b(x) = 3x + 2
r(x) = a(x) + b(x) 

...

'Implementation of A with High Cohesion (Almost Atomic)
r(x) = 5x + 3

...

'Implementation of A with Low Cohesion too
a(x) = 2x + 1
r(x) = a(x) + 3x + 2

...

'Implementations of A with Functional Cohesion
a(x, y) = x * y   'Groups multiplication
b(x, y) = x + y   'Groups addition
r(x) = b(a(5,x), 3)
4

3 回答 3

0

有趣的观点,你正走在理解更大概念的正确道路上。由于您引用了 Wikipedia 文章,您认为您的示例代表了列出的十种耦合类型中的任何一种吗?

我看到也许您的示例可以表示数据和/或消息耦合,但仅此而已,因为例如基本代数语句不维护状态(即存储值)。

首先要意识到这个简单的代数最终会过于简单,无法代表软件开发中所有重要的耦合模式。我建议立即向您的函数添加多个变量,并使用不同的变量名称等,以增加函数可以卷积的可能方式。

为了说明其他一些耦合模式,您至少需要某种方式来存储值并引用以前存储的值......我们在解决代数问题时都会做的事情,但不是用通用语法或结构教授的事情。(回想一下草稿纸和页边空白处的工作问题来解决问题?可能没有人教你一个严格的系统来做这件事,也没有人人都遵循。)当你开始在你的例子中添加这些类型的功能时,只是为了说明不同的耦合,你基本上会得到一个相当于你自己的编程语言的东西。根据您的意图,这可能是一个非常值得和愉快的练习,

于 2017-03-31T23:03:29.953 回答
0

进一步阅读(感谢Cade Perkins),看来我错了。

这些例子实际上是关于凝聚力的(根据维基百科)

'Implementation of A with Low Cohesion
'(Random cohesion because there is )
' no reason to group the functions
' in this way)
a(x) = 2x + 1
b(x) = 3x + 2
r(x) = a(x) + b(x) 

...

'Implementation of A with High Cohesion (Almost Atomic)
r(x) = 5x + 3

...

'Implementation of A with Another Random Cohesion
a(x) = 2x + 1
r(x) = a(x) + 3x + 2

...

'Implementation of A with Functional Cohesion
a(x, y) = x + y       ' Groups Addition
b(x, y) = x * y       ' Groups Multiplications
r(x) = a(b(5, x), 3)
于 2017-04-01T00:50:50.027 回答
0

用非常简单的术语 - 耦合表示一个类与程序中其他类的连接数。它拥有的越多,耦合度就越高。理想情况下,越少越好。

于 2021-08-12T11:16:46.687 回答