在UML类图中,我们是否对两个类应用相同的操作?

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

Do we apply the same operations in both classes in a UML class diagram?

问题

我对类图变得非常焦虑。假设我有一个名为Investigation的类。在那个类中有一个名为share的操作,允许用户分享调查。假设我还有一个名为User的类。用户是否应该也有一个名为share的操作,因为用户可以分享调查?我感到非常困惑。关联关系会解决这个问题吗?

英文:

I'm getting really paranoid about class diagrams. Let's say I have a class named Investigation. In that class there is an operation named share which allows a user to share the investigation. Let's say I also have a class named User. Should the class User also have an operation named share because the user can share the investigation? I'm getting really confused. Will association fix this?

答案1

得分: 2

如果UserInvestigation之间存在结构性的语义关系,例如用户被分配了调查,调查由某些用户拥有,或者用户有一个关联列表来浏览,那么您需要一个关联。

如果存在一种瞬时关系,例如用户可以浏览所有的调查,并不时决定共享其中一个,而不保留任何痕迹,那么就不需要关联。

现在,如果Investigation有一个share操作,User可以简单地使用该操作,无论他们是否与调查相关联。

然而,问题是另一个类是否需要告诉User共享一个调查(例如,用户界面的类)。在这种情况下,您可能会考虑为User添加一个share操作。但如果您从这样的设计开始,您需要考虑最小知识原则,因为这样的设计可能导致大量的隐藏耦合,使软件难以在未来演化方面进行维护。

英文:

You would need an association between User and Investigation if there is a structural semantic relationship between the two, for example if users have investigations assigned, if investigations are owned by some users, or if users have a list of associations to browse.

If there is a transient relationship, e.g. if users can browse all the investigations, and from time to time decide to share one, without a trace being kept, there would be no need for an association.

Now if Investigation has a share operation, User could simply use that operation, whether there are associated with the investigation or not.

However the question arises if another class would need to tell User to share an investigation (e.g. a class of the user interface). In this case, you may be interested in having a share operation for User. But if you start like this, you'd need to have a look at the principle of least knowledge, because such a design could lead to a lot of hidden coupling that makes the software difficult to maintain in regard of future evolutions.

答案2

得分: 0

这取决于类图的目的。

如果类图是对现实世界的建模,那么操作应反映类的实例的行为。在现实世界中,调查不会分享自己。相反,一个人会分享它。 '分享' 是 Person 的一个操作,它有一个调查作为参数。

如果类图是对应用程序运行方式的建模,那么操作应反映由类表示的软件部分的行为。在应用程序中,User 和 Investigation 是某种面向对象编程语言中的类。类的操作意味着在某个事件发生时被调用,例如当按下按钮时。也许 Investigation 有一个布尔属性 'isShared'。在这种情况下,'分享' 操作应在 Investigation 类上以将属性设置为 true。或者,更不太可能的情况是,类 User 可能拥有一个共享调查的列表。在这种情况下,'分享' 操作应在 User 类上以将一个调查添加到列表中。

这只是类图可能存在的两个目的之一。还有更多。确定你的类图的目的,然后做出你的选择。没有一个单一的正确解决方案。

英文:

It depends on the purpose of the class diagram.

If the class diagram is a model of the real world, then the operations shall reflect the behaviour of the instances of the classes. In the real world, investigations don't share themselves. Instead, a person shares it. 'Share' is an operation of Person and it has an investigation as a parameter.

If the class diagram is a model of how an application works, then the operations shall reflect the behaviour of the piece of software represented by the class. In the application, User and Investigation are classes in some object-oriented programming language. The operations of the classes are meant to be called at some event, e.g. when a button is pressed. Maybe Investigation has a boolean attribute 'isShared'. In that case, operation 'share' shall be on class Investigation in order to set the attribute to true. Alternatively, less likely, class User may own a list of shared Investigations. In that case, operation 'share' shall be on class User in order to add one investigation to the list.

These are only two possible purposes of the class diagram. There are more. Determine the purpose of your class diagram and then make your choice. There is no single correct solution.

huangapple
  • 本文由 发表于 2023年6月25日 19:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550117.html
匿名

发表评论

匿名网友

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

确定