Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UML association class in multiple relations

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!

like image 645
tremon Avatar asked Apr 11 '21 18:04

tremon


People also ask

Can you have multiple association classes?

An association relationship must be bi-directional, and optional in each direction. (Neither end of the relationship can have a multiplicity value of "one" or "one or more".) You can define multiple associations between the same two classes, if appropriate.

What is association class in UML?

An association class is identical to other classes and can contain operations, attributes, as well as other associations. For example, a class called Student represents a student and has an association with a class called Course, which represents an educational course. The Student class can enroll in a course.

What is multiplicity for an association in UML?

Multiplicity can be set for attributes, operations, and associations in a UML class diagram, and for associations in a use case diagram. The multiplicity is an indication of how many objects may participate in the given relationship or the allowable number of instances of the element.

What are the different types of association in UML?

An association can be categorized into four types of associations, i.e., bi-directional, unidirectional, aggregation (composition aggregation), and reflexive, such that an aggregation is a special form of association and composition is a special form of aggregation.


2 Answers

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).

like image 120
Christophe Avatar answered Sep 18 '22 04:09

Christophe


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.

like image 21
bruno Avatar answered Sep 22 '22 04:09

bruno