Class Diagram , composition relationship - uml

I have three classes:
-User class
-Role class
-UserRole class : to join both classes
Each user can have multiple roles.
So, I made this class diagram:
Is the composition relationship between classes and the cardinalities correct?

No. When the relationship goes away, you don't want the other things to get deleted. The pointy ends must also be 1's.

You can safely omit the composition diamonds as they add no real semantic to the model. Compositions are used on a metaphoric level and are more food for philosophers than anyone else.
Now for the multiplicities. A UserRole binds exactly one user to one role. You can have multiple UserRoles to reflect different roles for single or multiple users. So you should place a 1 to the left and right multiplicities.

Related

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:
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!
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:
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)
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.
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).
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.
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}.

UML diagram multiplicity notation

I have a question about my UML diagram. I have made a system consisting of a "User" parent class and the two child classes inherit from the "User". Now my question is how to show the multiplicity notation between them, just like the multiplicity notation is shown between "Booking" and "client", where more clients can only have one booking each and the notation would be many to one.
The question is:
The user in our system can only be a practitioner or a client, not both at the same time -->, therefore, the notation would be 1 to 1
There are more users and the different users could be either a practitioner or a client --> therefore the notation would be 1..n to 1..n
What is the right way to write it? Is it wrong to write multiplicity notation when a class inherits? Please help I have spend like 2 hours on figuring it out.
You don't. There are no multiplicities on a Generalization in UML.
The fact that your User is either a Cient or Practitioner can be expressed by setting your User abstract. The notation for abstract classes is to set the name in italic. This way you cannot instantiate User instance directly, but you have to use one of the concrete subclasses.
The second phrase doesn't really constrain anything, so you don't have to mention it at all.
Remark: the guillemets « and » are reserved in UML to denote stereotypes and keywords. As it is right now your User class is really an unnamed class with stereotype «User»

How the N-ary association works in class diagram?

i have three classes and each one of them has an association with same forth class, is it okey to use N-ary association in this case ?
It depends on the logic of your domain. If each of the three classes has a logically separate relationship with the fourth class (i.e. they can vary independently) then they are separate associations. If they are all associated by the same relationship then this would be N-ary. For example, a Car, Driver and Route could be all associated one relationship -- that you might call Journey -- which would be three-ended (N-ary), whereas a Car and an aggregate part (e.g. Wheel) would be two-ended. So it depends.

Multiple compositions in UML

In a UML class diagram, is it technically correct to have two possible compistion relationships leading to one class?
I.E. I have an inventory class, which has a composition of the inventory class. I want to have the same relationship but with a container class taking the place of the inventory.
So, can I have two compositions, or do I need to turn these into aggregations?
You can have as many composite associations as you like on the class level. But each instance can only be part of one composition at a specific moment in time.
UML superstructure says:
If the whole has aggregationKind = composite then the part can be included in at most one composite at a time
This article I wrote tries to explain the difference: UML Composition vs Aggregation vs Association
Any number of composition association can lead to one class of course. If instance of composed class is composed of instances of more types for example.

Class diagram relationship diamonds

When creating relationships in an Object Class Diagram for an Object Relational Database, should the diamonds on the ends of the relationship links be filled in or not.
Here is an image of my Class diagram:
http://canning.co.nz/Weltec/Class_Diagram.png
It's a choice between Composition and Aggregation, which Wikipedia explains quite well.
In practice though, I think a valid answer is to just not worry about the difference, unless it's a school assignment. I've found that trying to make very detailed UML diagrams isn't terribly useful in practice.
Composition relationship has a coloured diamond shape structure ending at a class if it belongs to a 'is a' relationship (i.e- the entity cannot exist without the parent class) whereas the aggregation has an empty diamond shape ending at a class if it belongs to a 'has a' relationship (i.e- the entity can exist without the existence of the parent class.
A full diamond denotes Composition, or a 'owns' relationship. You use it when the referenced entity can't exist without the class representing it. An example would be order to order item. The order item just doesn't make sense without the order.
An empty diamond denotes Aggregation, or a 'has' relationship. A quick glance at your diagram makes me think this is the correct diagram element to use in your case.
But I agree with #mpartel: If there aren't any specific requirements to distinguish between the two just ignore the diamonds.

Resources