• 4 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 2/18/22

Implement the Model for Your Application

Now for the fun part: to apply SOLID principles and MVC architecture, we’re going to create a simple card game application together. We will create a deck of 52 standard playing cards. We will have a selectable number of players, deal a card to each player with the highest card winning. 🃑 🂱 🃁

First, we begin coding, making changes as we go along. We will then change the rules as well as the deck of cards. We’ll add a GUI (graphical user interface). If we follow SOLID and MVC, the impact of our changes will be minimal.

Here are the requirements for the application.

Gameplay specifics:

  • Create a standard 52 card deck.

  • Enter player names. Limit the number of players to five.

  • Shuffle the deck of cards.

  • Deal one card to each player (face down).

  • Flip over all player’s cards, showing what card they have.

  • Check which player has the highest ranked card: Ace > King > Queen > Jack > 10 > . . . 2.

  • If tied, winner is based on suit: 

    Clubs > Spades > Hearts > Diamonds

  • Show winning player name and card.

  • Put all cards back into the deck.

  • Jump back to shuffle step.

How Do You Plan Out the Model?

Let's find the primary items players will view and interact with. The most straightforward way to find model objects is to read through the requirements, looking for the nouns. So looking at the description, you find a card deck (a collection of playing cardsplayers, card rank,  and card suit.

But how do we connect players and their cards together? A player is going to possess a card during the game, right?

While not explicitly mentioned, the single card for each player goes in a hand class. It stays true to single responsibility. A player consists of their name and hand. In this way, if the rules change, it's probably in the number of cards a hand holds, or something about the way the cards are manipulated. You shouldn't have to change the basic model of a player if you play a different game.

How Do You Implement the Model? 

Now you will need to create Java classes for each of the model elements. Let's do it together!

Step 1: Generate Rank and Suit

We've created:

  • Rank.java (an enumeration of 2 through ace, with ace being the highest)

  • Suit.java. (an enumeration of suits, with clubs being the highest)

Step 2: Generate Playing Card

We've created:

  • PlayingCard.java (a rank, suit and whether the card is face up or down)

Step 3: Generate Deck

We've created:

  • Deck.java (a collection of playing cards)

Step 4: Generate Hand and Player

We've created:

  • Hand.java (a collection of playing cards)

  • Player.java (a name and a hand)

As we begin to further implement the game, following SOLID principles, we can modify these ideas. In the next chapter, we begin work on the controller and a command-line view.

Let's Recap! 

  • The model consists of the elements with which you interact. These contain the state information of the system.

  • To find your model objects, review the list of requirements.

  • In our application, we've defined that:

    • The model consists of player, hand, playing card, deck, rank, and suit.

    • A player has a hand. A hand holds a playing card. A deck holds playing cards.

In the next chapter, we will begin to control the sequence of the game events.

Example of certificate of achievement
Example of certificate of achievement