4

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 Chat is 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

bruno
34.3k8 gold badges31 silver badges48 bronze badges
asked May 15, 2025 at 14:21
5
  • The word composition matters. 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-out users that have deleted their accounts. You should probably look at aggregation instead of composition. Commented May 15, 2025 at 14:32
  • @PanagiotisKanavos "A user isn't composed of chats" this is very true, and because of that "You should probably look at aggregation instead of composition" is wrong. You probably wanted to say "simple association" or something like that rather than "aggregation" ;-) Commented May 15, 2025 at 14:56
  • 1
    @PanagiotisKanavos you are right in principle, but formally, composition is the short form for "composite aggregation", which is no longer defined in terms of part/whole but in terms of lifecycle ownership. So composition for ownership relations is a valid model nowadays (even if I'd recommend to avoid it as much as possible). Anyway, a component is not allowed to be part of more than one composite, so it's not allowed to have a multiplicity 2 on the diamond side ;-) Commented May 15, 2025 at 17:22
  • 2
    @jenepix this is an interesting model, beside the discussion on the composite, I'd just like to highlight that nothing in your model guarantees that the user linked to a message is one of the users owning the chat (this also raises the question if users owning a chat could evolve over time, or if a constraint should be added to ensure consistency. Is there any reason for showing navigability explicitly ? Commented May 15, 2025 at 17:27
  • 2
    @Christophe Thanks, great point! you're right, the model doesn't guarantee that the sender of a message is one of the chat participants. I'll add a constraint for that. in my current use case, chats always involve exactly two fixed users, so maybe I’ll note that as well. i added navigability just for clarity while thinking through the structure, but you're right, it's not strictly necessary. also, I didn’t realize that composition implies exclusive ownership, That's very helpful to know! Thanks again for the helpful feedback! Commented May 15, 2025 at 18:01

1 Answer 1

2

each User can be part of multiple Chats

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 :

enter image description here

Out of that, are you sure a Message must know in which Chat it is ? If no Chat <*>-1----*-> Message is enough :

enter image description here

answered May 15, 2025 at 14:33
Sign up to request clarification or add additional context in comments.

8 Comments

Can a chat be in a composition with multiplicity 2, when a composition is about exclusive ownership ?
@christophe you are very true, I modify my answer
@Christophe fortunately you are here, UML starts to be so far for me ...
9.9.17.8 even puts this constraint black on while: "multiplicity_of_composite: A multiplicity on the composing end of a composite aggregation must not have an upper bound greater than 1. inv: isComposite and association <> null implies opposite.upperBound() <= 1"
Thanks a lot, this really helped clarify things. I didn’t realize that an object can only be part of one composite. That really makes it clear why using composition between User and Chat is incorrect. and you're totally right, a Chat isn't a part of a User. It does make sense for Message and Chat though, I just didn't connect the dots and applied the same logic to users by mistake. Thanks again for the detailed explanation and for pointing to the UML spec too much appreciated!
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.