114
Chapter 6 Designing with Objects
n
Cards and card—This one follows the same logic as player.We absolutely need
cards in the game, but we want the class to represent a single card, so we strike
cards and keep card.
n
Deck—Because there are a lot of actions required by a deck (like shuffling and
drawing), we decide that this is a good choice for a class.
n
Hand—This class represents a gray area. Each player will have a hand. In this game,
we will require that a player has a single hand. So it would be possible for a player to
keep track of the cards without having to have a hand object. However, because it is
theoretically possible for a player to have multiple hands, and because we might want
to use the concept of a hand in other card games, we will keep this class. Remember
that one of the goals of a design is to be extensible. If we create a good design for
the blackjack game, perhaps we can reuse the classes later for other card games.
n
Face value—The face value of the card is best represented as an attribute in the
card class, so let’s strike this as a class.
n
Suit—Again, this is a gray area. For the blackjack game, we do not need to keep
track of the suit. However, there are card games that need to keep track of the suit.
Thus, to make this class reusable, we should track it. However, the suit is not a good
candidate for a class. It should be an attribute of a card, so we will strike it as a class.
n
Ace—This could better be represented as an attribute of the card class, so let’s
strike it as a class.
n
Face Card—This could better be represented as attribute of the card class, so
let’s strike it as a class.
n
King—This could better be represented as attribute of the card class, so let’s strike
it as a class.
n
Queen—This could better be represented as attribute of the card class, so let’s
strike it as a class.
n
Bet—This class presents a dilemma.Technically you could play blackjack without a
bet; however, the requirements statement clearly includes a bet in the description.
The bet could be considered an attribute of the player in this case, but there are
many other games where a player does not need to have a bet. In short, a bet is not
a logical attribute of a player.Also abstracting out the bet is a good idea because we
might want to bet various things.You can bet money, chips, your watch, your horse,
or even the title to your house. Even though there might be many valid arguments
not to make the bet a class, in this case we will.
We are left with six classes, as shown in Figure 6.5.