XP Home

Refactor the Simulator Class


We have kind of a mess on our hands right now. We got the test to run, but the code isn't very pretty. So let's do some refactoring. First we always run the unit tests to make sure we have 100%, then we start. We can add lots of small methods named for what they do.

package simulator.r8;

public class Simulator extends Thread
{SimulationInterface gui;
boolean boilerIsOn = false;
static final int BoilerSwitch = 0x1000;

public Simulator (SimulationInterface aGUI)
gui = aGUI;}

public void run()
{while (true)

private void checkBoilerSwitch()
{if (wasBoilerJustSwitchedOn()) turnOnBoiler();}

private boolean wasBoilerJustSwitchedOn()
{return isBoilerSwitchedOn() && boilerIsOff();}

private boolean isBoilerSwitchedOn()
{return (PIA.register & BoilerSwitch) > 0;}

private boolean boilerIsOff()
{return !boilerIsOn;}

private void turnOnBoiler()
boilerIsOn = true;}

private void sleepOneTenthSecond()
catch (InterruptedException exception)
You know what comes next. Run those unit tests again. They still run so we have not broken anything. Let's add another unit test. Spike Solution

ExtremeProgramming.org home | A Spike Solution | Next Unit Test |

Copyright 1999 by Don Wells.