Skip to content

Extract Hand Class

Goal

In this lab, you'll fix Primitive Obsession by doing an Extract Class refactoring of a new Hand class. What you'll end up with is playerHand and dealerHand both being instances of this new Hand class instead of referring to the List<Card> collection.

Run All Tests...

Make sure all the tests pass before you start.

Steps

Try to follow these steps to extract the Hand class from the Game class. Make sure the tests continue to pass as you do the refactoring steps. You may need to update the structure of tests along the way.

  1. Copy and Paste the List<Card> hand field to a new class called Hand. Hand will have only one List<Card> collection. (Recommend naming this collection cards.)

  2. Replace the List of Card with an instance of the new Hand class.

Do this for both the playerHand and the dealerHand.

  1. In Hand: create a getter for access to the collection.

    Note: this getter is temporary!

  2. In Game: do a Search & Replace to replace the use of the original collection with the getter from the Hand class.

  3. Find usages of Hand’s collection getter (using CMD+B or CTRL+B on the get method).

  4. For each usage found, use Extract Method and Move Method refactorings (and possibly Inline Method and Introduce Parameter) to move the usage to the Hand class

  5. Continue finding usages and moving methods until there is no more external use of the collection getter

  6. Inline & remove getter from the Hand class

Run All Tests...

Make sure all the tests pass when you are done.


You are done!