I’ve come to really like the pattern of extracting local or instance variables into their own State
class, and writing an algorithm in terms of that State
.
A really trivial (if contrived) example of this would be a “sum cubes” algorithm; something like this:
public class CubeSummer {
private final PrintStream printStream;
private long i;
private long sum;
public CubeSummer() {
this.printStream = System.out;
reset();
}
public long sumCubes(long limit) {
for (; i<limit; i++) {
sum += i * i * i;
display();
}
return sum;
}
public void reset() {
i = 0;
sum = 0;
}
public void display() {
printStream.print("i = " + i + ";");
printStream.println(" sum = " + sum);
}
}
What’s happened here is we’ve essentially promoted some variables that should arguably be local variables into instance variables, to avoid needing to pass them around explicitly.
But in the process, we’ve also mixed long-lived state with short-lived state, broken thread safety, and forced clients of this class into a particular calling convention (either instantiate an instance per sumCubes
call or carefully respect thread safety and call #reset()
.)