没有一个优先的类之间关系吗?为什么?

huangapple go评论72阅读模式
英文:

is there a preferable relationship between classes? Why?

问题

在类之间真的存在一种更可取的关系吗?还是这取决于我们拥有的软件?
我知道在类之间有 is-a 和 has-a 的关系,但是,在软件设计师之间是否存在一种关系是确凿无疑且最可取的。

英文:

is there really a preferable relationship between classes ? or it depends on the software we have ?
I know that we have is-a and has-a relationships in classes relations but, is there a one relation that is Confident and it most preferable between software-designers.

答案1

得分: 3

类之间唯一优先的关系是独立性。因为独立性意味着确保关注点分离和自由演化。(笑话)

但不幸的是,独立性并不是很有用:许多孤立的类只会帮助解决许多小的孤立问题。如果你真的想要做些有用的事情,你将不得不关联正确的类。然后,唯一重要的是哪种关系能最好地满足你的需求。有时候是继承(is-a),有时候是组合(has-a)。这完全取决于上下文。

你的“医生”可能的意思是更倾向于**组合而非继承。这是一个有用的建议。但这只是一个简单的经验法则:它不是普遍真理**。

英文:

The only preferred relationship between classes is independence. Because independence means guranteed separation of concerns and freedom to evolve. (Joke)

But unfortunately independence is not very useful: Lots of lonesome classes will only help to solve lots of little isolated problems. If you really want to make something useful, you'll have to relate the right classes. And then, the only thing that matters is what relationship helps you to best address your needs. Sometimes it's inheritance (is-a), sometimes it's composition (has-a). It all depends on the context.

What your "doctor" probably meant was to prefer composition over inheritance. This is a useful advice. But it is a simple rule of thumb: it is not a universal truth.

huangapple
  • 本文由 发表于 2020年10月6日 01:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64213226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定