What is the relationship between a card and deck in a Class Diagram (Texas Hold 'em)? I'm still trying to understand how the relationships work in a class diagram. If a deck consists of cards, wouldn't that be a composition considering that removing a deck would remove the cards? There is the case where you can remove a deck but still have a card and this would be considered aggregation. So my mind is boggling with this concept. Can someone explain this to me with an example?
-
In a class diagram, probably no relationship. A `Deck` class might have a property `Cards` that was a collection of some type where the item type was `Card`. – jmcilhinney Sep 29 '14 at 23:29
2 Answers
Developers lose sight of the intent of OO. A class diagram should resonate with the real-world domain. In the domain of Texas Hold 'em, a Deck contains up to 52 Cards, and a Card is contained in up to one Deck. A Card can also be held in up to one Hand, or laid on the Table, if I recall correctly. This is what you should be modeling, not the implementation details.
While one could model the relationship between a Deck and the Cards as a composition, I doubt that adds any value. When was the last time you set fire to the Deck, taking all the Cards with it? Essentially, you add and remove Cards from the Deck and you shuffle the Deck, until the game is over.

- 6,177
- 3
- 21
- 47
Based on the description of Jim L. we could make the following domain model:
In this model, the association between Deck
and Card
is modeled as a composition because a card is part of a deck and cannot be shared with another deck (notice that non-shareability is the defining characteristic of composition, and not the life-cycle dependency).
I'd like to add to what Jim L. said that a class diagram can be used on all three levels of modeling:
- (solution-indepnedent) domain modeling
- (platform-independent) design modeling
- (platform-specific) data modeling

- 5,481
- 1
- 22
- 41