I'm designing a UML class diagram for a messaging app.
Each Chat
must have exactly 2 Users, and each User
can be part of multiple Chats
. I originally modeled this with a composition from User
to Chat
.
However, I'm not sure if this is correct.
My goal:
Chat
is between exactly two Users.Should I be using a normal association instead of composition here? What’s the best way to represent this in UML?
Thanks in advance!
each
User
can be part of multipleChats
so you have to replace the multiplicity 1
by *
(or 0..*
if you prefer) on the composition User / Chat
Should I be using a normal association instead of composition here
pro of a composition : the composition indicates that If both users are deleted, their chat is also deleted
cons of a composition : the main is a composite aggregation is a strong form of aggregation that requires a part object be included in at most one composite (c.f. formal/2017-12-05 §9.5.3 Semantics page 112), and also independentely of the multiplicity a composition is first an agregation and a Chat is not a part of a User
But notice the multiplicity 2 on the association User / Chat implies a Chat needs 2 associated Users and cannot exist without them, restricting its life.
So for me you must use a normal/simple association :
Out of that, are you sure a Message must know in which Chat it is ? If no Chat <*
>-1----
*-> Message
is enough :