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.
-
Copy and Paste the
List<Card>
hand field to a new class calledHand
. Hand will have only oneList<Card>
collection. (Recommend naming this collectioncards
.) -
Replace the List of Card with an instance of the new
Hand
class.
Do this for both the playerHand
and the dealerHand
.
-
In
Hand
: create a getter for access to the collection.Note: this getter is temporary!
-
In
Game
: do a Search & Replace to replace the use of the original collection with the getter from theHand
class. -
Find usages of Hand’s collection getter (using
CMD+B
orCTRL+B
on theget
method). -
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 -
Continue finding usages and moving methods until there is no more external use of the collection getter
-
Inline & remove getter from the
Hand
class
Run All Tests...
Make sure all the tests pass when you are done.
You are done!