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?

- 68,716
- 7
- 72
- 138

- 45
- 9
2 Answers
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.

- 68,716
- 7
- 72
- 138
-
1Thank you. That's made it easier to understand. – coder Jun 25 '23 at 12:06
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.

- 5,768
- 1
- 17
- 32