1

I'm currently trying to learn UML but I have trouble with this one. Lets say you have a container with cards, a container with players, and that player has also cards. How would you draw this in a class diagram (application model). How would you set the composition and multiplicity? Thanks in advance.

I was thinking about this:

enter image description here

But The problem is that I now have 2 lists of cards.

  • I added my guess. Feedback is appreciated – Roy Oct 5 '14 at 11:56
  • Code is precise and executable. UML diagrams are neither. They are only useful to communicate ideas to people. Don't dwell on their precise semantics. I've been in this business three decades and class diagrams rarely last longer than one meeting. Even if they are saved in design documents, when the code must be changed, it's the code that is read. – kevin cline Oct 6 '14 at 16:08
1

In principle, there is nothing wrong with your UML diagram.
However, it does suggest to me that the Cards referenced by the Player class are different instances than those held in the Deck. This might be an artefact of how I interpret an aggregation relation, for which there is no consensus.

With regard to the members shown in the classes,I am assuming that the semantics are based upon languages like Java and C#: Objects live all on the heap and are only accessible through references.

Depending on what you want to model, you can improve the diagram in two ways:

  • If a Card that gets dealt out is removed from the Deck and moved to the Player that receives the Card, I would make the link between Deck and Card an aggregation instead of a composition.
  • If the Player class refers to Card classes that are part of the Deck, then I would make the link between Player and Card a simple association, to signify that the Player doesn't affect the lifetime of the Card.

With respect to the multiplicity, you can leave it out on the diamond-side of a composition relation, because it should always be 1.

If you compare your diagram to others, you may find that often the class members corresponding to associations/aggregations/compositions are left out, because they don't add any information. Also, multiplicities are often left out of the diagram, especially if they can be inferred from the context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.