6

I'm making a class diagram in UML, and I can't find information about this topic. Can I have a class in several associations-classes? here's an example:

example model

Message should be an association class between user and group, but also between user and channel. Is this allowed or is there any other way to do this? thank you!

tremon
  • 85
  • 5
  • warning *Can I have the same association class associated with multiple relations?* is wrongly said and you must at least remove *association*, and same in title. You want something like *Can I have a class in several associations-classes* – bruno Apr 11 '21 at 20:39

3 Answers3

2

What is an association class?

When looking at the graphical notation of an association class, we could be mislead to think that an association-class is simply a class that is attached to an association.

But the association class is in reality an association AND AT THE SAME TIME a class:

UML 2.5.1 / Section 11.5.3.2: An AssociationClass is a declaration of an Association that has a set of Features of its own. An AssociationClass is both an Association and a Class, and preserves the static and dynamic semantics of both.

So in the modelling semantic, beyond the notation, you cannot separate the association class from the corresponding association. If you're not convinced yet, here the next sentence in the specifications:

An AssociationClass describes a set of objects that each share the same specifications of Features, Constraints, and semantics entailed by the AssociationClass as a kind of Class, and correspond to a unique link instantiating the AssociationClass as a kind of Association.

(links are instances of an association and correspond to tuples with "one value for each memberEnd of the Association")

What are the consequence of the association-class duality?

The consequence is that the same association-class cannot exist in multiple flavors that would each associate different sets of classes.

While nothing in the notation prevents you from adding a doted line to seemngly "attach" the same class to two different associations as Bruno explains, this notation would not correspond to a valid UML semantic.

Alternatives

Your question underlines an interesting design issue. And fortunately, there are plenty of ways to solve it. For example:

  1. User class is associated to an abstract Destination class. Message would be the association-class. Destination would be specialized into Group and Chanel that would both inherit the association (no need to duplicate the association graphically). (Edit: as underlined by Axel Scheithauer in the comments, the association and the association class being one and the same, they must have the same name)

enter image description here

  1. Forget about the association class. Make Message a normal class, associated with User. Associate it also with Group and Chanel. If necessary, add an {xor} constraint between these two associations if they are mutually exclusive.

enter image description here

The fact that you currently have a many-to-many association only with Group and not with Channel, suggest that there are some significant differences and would speak in favor of (2) rather than (1).

Not related: Something weird in your current model?

Regardless of the association-classtopic, you current model raises some questions regarding the many-to-many association with Group:

  • do you meant that several users can send a same message to several groups?
  • or do you mean that a user can send messages to a group and the group is made of several users?

In the latter case, you should go for 2 distinct associations: one for the sending association, and one for the group membership association (see the red association in the diagrams above).

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    You explain correctly that the association class specifies_one_ object that is an association and a class at the same time. However, in your example diagram you show two different names `sends` and `message`. Since they are the same element, they can only have one name. – Axel Scheithauer Apr 16 '21 at 14:31
  • @AxelScheithauer You’re fully right of course! I used the elements as they were in OP’s question for helping the mapping. I have edited, to draw attention on your important remark and address the issue in the graphical notation. – Christophe Apr 17 '21 at 10:34
1

This is a very interesting question.

In formal/2017-12-05 there is nothing in Figure 11.25 Associations page 199 nor in § 11.5.3.2 Association Classes starting page 200 nor in § 11.8.2 AssociationClass [Class] starting page 220 saying a class cannot be used for several associations-classes.

So for me it is allowed to have

enter image description here

but warning, the name of the class and the name of the association must be the same, from formal/2017-12-05 § 11.5.3.2 Association Classes page 200 :

Both Association and Class are Classifiers and hence have a set of common properties, like being able to have Features, having a name, etc. These properties are multiply inherited from the same construct (Classifier), and are not duplicated. Therefore, an AssociationClass has only one name, and has the set of Features that are defined for Classes and Associations.

Then the class cannot be named Message and the associations sends if you want to make association-class.

Note class and an association are NamedElement7.8.9 NamedElement [Abstract Class] from page 47), a given name can be used for several NamedElement but to co-exist in the same Namespace two NamedElements must be distinguishable. From formal/2017-12-05 § 7.8.9.7 Operations page 49 :

isDistinguishableFrom(n : NamedElement, ns : Namespace) : Boolean
The query isDistinguishableFrom() determines whether two NamedElements may logically co-exist within a Namespace. By default, two named elements are distinguishable if (a) they have types neither of which is a kind of the other or (b) they have different names.

Then the two associations Message must be in different namespaces because they have the same name.

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1
    Thank you very much, this solved my problem :) I was going crazy trying to find this! – tremon Apr 11 '21 at 20:56
  • @tremon you welcome, I hope I am right, will see with reaction of other people. – bruno Apr 11 '21 at 20:57
  • @tremon your question has a counterpart, I put the question [Is it possible to use an association in several associations-classes?](https://stackoverflow.com/q/67050157/2458991) ;-) – bruno Apr 11 '21 at 21:05
  • I challenge you to draw instance specifications of what in the UML meta model would be instantiated for what you have drawn. – Jim L. Apr 12 '21 at 00:38
  • 2
    An AssociationClass is still an Association. Therefore the same rules apply: A binary Association or AssociationClass connects two classes. That means your model is not possible. You could have a ternary Association, but that would be a completely different thing, with a completely different notation (a white diamond shape connecting to all three and with a dashed line to the class part of the AssociationClass). So, I think @JimL's answer is the way to go. – Axel Scheithauer Apr 13 '21 at 13:00
  • @AxelScheithauer *a binary Association or AssociationClass connects two classes* yes, and what ? There is no ternary association, there are two association-class, the fact one way to implement an association-class is to use a ternary association changes nothing. I do not understand your remark – bruno Apr 13 '21 at 17:25
  • @bruno: In your diagram there are two lines resembling Associations and one rectangle resembling a Class connected with dashed lines. If there were only one dashed line, it would be the symbol for an AssociationClass. I don't know, what you want to express with your notation. Since you connected the three Classes Group, Channel and User, I wondered whether you had a ternary AssociationClass in mind. – Axel Scheithauer Apr 15 '21 at 20:32
  • @AxelScheithauer there are 2 separated dashed lines, to make 2 associations-classes – bruno Apr 16 '21 at 07:14
  • @bruno These two separate dashed lines must be connected to two separate rectangles. Then you have two association classes. You can have a generalization between them to show that they have something in common, but you need two rectangles. – Axel Scheithauer Apr 16 '21 at 14:23
  • @AxelScheithauer may be, may be not, of course your insistence sows doubt in my mind ... but not enough ;-) – bruno Apr 16 '21 at 14:29
0

If you want Message to be an instance of a UML Association Class that connects User to Group and Channel, you can connect one end property of the Message association to an instance of a UML Class that is the union of Group and Channel.

To construct a union class, make it the general end of two instances of UML Generalization and make it abstract. The specific end of one generalization would be Group, and the other would be Channel. For extra clarity, put the generalizations into an instance of a UML Generalization Set that is {complete}.

Jim L.
  • 6,177
  • 3
  • 21
  • 47
  • 1
    That was something I had thought about, but the problem is that group and channel have different cardinalities ok their relations – tremon Apr 12 '21 at 07:11
  • What are English sentences you’re trying to model? I think you’re going about this in a way that does not communicate your intent to others. – Jim L. Apr 12 '21 at 12:11
  • it would be something like this: "The system will provide the functionality of groups and channels, a group is composed by a set of users, and everyone who is part of the group can send messages on it. A channel on the other hand, is slso composed by a set of users, but only the admin can send messages. A message is characterized by an id and the date it was sent. A user can also send messages to another user directly, without them being part of the same group or channel – tremon Apr 13 '21 at 14:34
  • 1
    Different multiplicities can be achieved by specializing the inherited Association. Yes, that is possible. – Axel Scheithauer Apr 13 '21 at 15:12
  • 1
    Why do you want to use AssociationClasses in the first place? From your description a message is the information _flowing_ from one instance of User to one instance of Group. For this case AssociationClasses are not the right way. Rather `message` will be an independent class with associations to `User` and to the abstract superclass. – Axel Scheithauer Apr 13 '21 at 15:26
  • 1
    It would be possible to use an AssociationClass for `message`, but it would be awkward. And the Associations in your diagram are needed on top of that. AssociationClasses are usefull, when the link between two instances needs to have attributes (e.g. Company-Person might need `contract start date`). Even then, they can usually be replaced with a normal class and two associations. – Axel Scheithauer Apr 13 '21 at 15:32
  • (I interfere in your answer while I also answer, I hope this is not a problem) *If you want Message to be an instance of a UML Association Class* : this is not the question of the OP, *Message* is a class and not an instance of an Association Class. I do not understand in what you answer to the OP question, notice that a question like *Can I have... * basically waits for the answer 'yes' or 'no' ... more of course explanations ;-) – bruno Apr 13 '21 at 17:35
  • Sorry, I’m having trouble understanding you. The OP wanted Message to be an instance of a UML Association Class that connects three instances of UML Class. That’s not valid. – Jim L. Apr 13 '21 at 23:20