Designing the objects
Designing the card part
For this example, the fundamental object in the lists is the Card object. A card, in the real world, has several attributes.
It can be face-up or face-down.
It has a suit.
It has a value such as Ace, King, Queen, etc.
It has a rank with respect to other cards that varies with the card game being played.
Each of these attributes will be implemented in the reusable Card part.
We will also need to consider some other attributes that help to properly represent the cards visually to the user.
In a text list the part will refer to a card using its full name, such as "Ace of Hearts."
In a container a graphic will represent the card itself, or some attribute of the card. In this example we will use an icon to show the card's suit when it is face-up, or the back of the card when it is face-down.
The actions available for a card are quite limited.
It can be turned face-up.
It can be turned face-down.
It can be flipped, ignoring whether it is currently face-up or face-down.
Designing the card deck
There are various types of card decks. Many card games are played with a standard deck of playing cards. However, some card games, such as Pinochle use a specialized card deck. Regardless of the type of deck being used there are certain actions and attributes they have in common.
The deck itself can be turned face-up or face-down.
The deck can be empty.
The deck can be shuffled.
Cards can be removed from the deck or added back to the deck.
The cards in the deck have an order, implying the card deck will need to be an ordered collection.
We will implement each of these attributes and actions in a card deck part that can be used in virtually any card game.
One key design point that needs to be addressed is whether the card deck should subclass the Ordered Collection part or contain an ordered collection of cards as an instance variable. The advantage of subclassing the Ordered Collection part is that its methods can be used to reduce the work involved in implementing the card deck. The advantage of using an instance variable is that internally the card deck can be implemented as something other than an ordered collection, perhaps as an array. It also provides that card deck with the opportunity to control what gets added to the ordered collection.
We will implement the card deck using an instance variable to contain the ordered collection of cards.
Designing the playing card deck
With the design of the card and card deck complete, the design of the playing card deck becomes relatively straightforward. The playing card deck reusable part will subclass the card deck class and initialize it to contain the standard 52 cards of four suits.
Last modified date: 01/29/2015