Separating concerns via multiple inheritance
In the Choosing between inheritance and extension – the is-a question recipe,we looked at the idea of defining a Deck
class that was a composition of playing card objects. For the purposes of that example, we treated each Card
object as simply having a rank and a suit. This created a number of small problems:
- The display for the card always showed a numeric rank. We didn't see J, Q, or K. Instead we saw 11, 12, and 13. Similarly, an Ace was shown as 1 instead of A.
- Many games, such as, Blackjack and Cribbage assign a point value to each rank. Generally, the face cards have 10 points. For Blackjack, an Ace has two different point values; depending on the total of other cards in the hand, it can be worth one point or ten points.
How can we handle all of the variations in card game rules?
Getting ready
The Card
class is really a mixture of two feature sets:
- Some essential features, such as rank and suit.
- Some game-specific features, such as the number of...