In Class Exercise: Refactoring Chess Classes
Step 1: Getting familiar with the existing classes.
Step 2: Identifying Methods
- Identify within the methods in the classes any repetition that can
be either avoided or done with a helper method.
- Circle one instance of the repeated code segment. Next to it,
indicate a good Intention Revealing Name for the helper method. Also,
write down any values that are different each time and so should be
passed as parameters.
- Cross out the other occurences of the repeated code. Write a call
to the new helper method in the margin.
Step 3: Creating an approriate inheritance structure
- Consider the following three inheritance structures:
- Which structure corresponds to the current code?
- How do the other structures differ from the current structure in
terms of what they represent?
- What would be an appropriate (Intention Revealing Name) name
for the ? in the 3rd structure?
- Which structure would be most appropriate to represent pieces in a
chess game?
- Where would you add other chess pieces, such as a Knight, Bishop,
King and Queen to your drawing?
Step 4: Finishing Up
- Given the structure you chose in Step 3, identify which instance
variables currently in Pawn should be in each of your classes.
- Also, identify which methods should be in each of your classes.
- Are there any methods that can be generalized and so used in classes
where they are not currently used? Are there additional helper methods
you could identify?
- Are there methods that can be inherited from one class to another?
- Are there methods that should be redefined in a subclass?
- Are there any methods that belong only in a subclass and not in its
superclass?
This page was created by Kelly Schultz
and