我在this site上遇到了这个短语在 Eclipse 中代码度量的讨论中,专门讨论了“缺乏内聚性”的概念:
Cohesion is an important concept in OO programming. It indicates whether a class represents a single abstraction or multiple abstractions. The idea is that if a class represents more than one abstraction, it should be refactored into more than one class, each of which represents a single abstraction.
在此上下文中什么是“单一抽象”?
来自Difference between Encapsulation and Abstraction ,我知道抽象通常只是向用户显示必要的细节(通过使用接口(interface)和抽象类)。 这里:What is abstraction? ,我再次明白了抽象就是隐藏实现。
您如何将这些想法应用到该文章中使用的单一抽象术语?
最佳答案
在您引用的网站 ( cohesion ) 中定义的特定上下文中,“单一抽象”意味着一个概念。所以它与 Single Responsibility Principle 强相关- 一个类应该处理“一件事”。如果一个类处理不止一件事,它通常会有许多不同的方法和变量,这些方法和变量不一定属于一起,因此它的内聚性会很低。
这不是这个词用于此目的的常见用法,根据我的经验,它更可能用于推理抽象级别/层,就像您链接的其他 SO 问题一样。它们与不同的规则相关,即 Single Layer of Abstraction Principle .
关于class - "abstraction"在 "class cohesion"或 "class cohesiveness"代码指标的上下文中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338786/