UML Class Diagram – How to Draw This Diagram?

class-diagramuml

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.

Best Answer

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.

Related Topic