Reputation: 85
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:
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!
Upvotes: 6
Views: 1942
Reputation: 73366
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")
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.
Your question underlines an interesting design issue. And fortunately, there are plenty of ways to solve it. For example:
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)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.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).
Regardless of the association-classtopic, you current model raises some questions regarding the many-to-many association with Group
:
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).
Upvotes: 2
Reputation: 6529
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}.
Upvotes: 0
Reputation: 32586
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
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 NamedElement (§ 7.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.
Upvotes: 1