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:
- A
Chatis between exactly two Users. - Users are not owned by the chat and can exist independently.
- If both users are deleted, their chat is also deleted.
Should I be using a normal association instead of composition here? What’s the best way to represent this in UML?
Thanks in advance! UML
1 Answer 1
each
Usercan 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 :
8 Comments
inv: isComposite and association <> null implies opposite.upperBound() <= 1"
compositionmatters. It means the composed "parts" can't exist without their parent - singular. A user isn't composed of chats and as you said, the chat can exist if one of the users goes away - or maybe both, that's a business decision. There are SO questions and answers with grayed-outusersthat have deleted their accounts. You should probably look at aggregation instead of composition.