Part 3 of 6 · 3 min read
The pattern
One class per state.
GoF's intent for State is to let an object change its behaviour when its internal state changes, so that it appears to change its class. GoF's motivating example is a TCP connection, which answers an "open" request differently when it is closed than when it is established.
The key move is to turn each state into an object. A common State interface declares the operations. A ConcreteState class per state implements them. The Context holds the current state object and passes each request to it.
interface State {
State confirm(); State checkIn(); State cancel(); String name();
}
abstract class Base implements State {
public State confirm() { throw bad("confirm"); }
public State checkIn() { throw bad("check in"); }
public State cancel() { throw bad("cancel"); }
private IllegalStateException bad(String what) {
return new IllegalStateException("cannot " + what + " when " + name());
}
}
class Requested extends Base {
public State confirm() { return Booking.CONFIRMED; }
public State cancel() { return Booking.CANCELLED; }
public String name() { return "REQUESTED"; }
}
class Confirmed extends Base {
public State checkIn() { return Booking.CHECKED_IN; }
public State cancel() { return Booking.CANCELLED; }
public String name() { return "CONFIRMED"; }
}
class CheckedIn extends Base { public String name() { return "CHECKED_IN"; } }
class Cancelled extends Base { public String name() { return "CANCELLED"; } }
class Booking {
static final State REQUESTED = new Requested(), CONFIRMED = new Confirmed(),
CHECKED_IN = new CheckedIn(), CANCELLED = new Cancelled();
private State state = REQUESTED;
void confirm() { state = state.confirm(); }
void checkIn() { state = state.checkIn(); }
void cancel() { state = state.cancel(); }
String status() { return state.name(); }
}
A new booking reports REQUESTED. After confirm() it reports CONFIRMED, and after checkIn(), CHECKED_IN. Calling cancel() then throws "cannot cancel when CHECKED_IN". On a fresh booking, checkIn() throws "cannot check in when REQUESTED", while cancel() works and leaves CANCELLED.
Look at what changed. Each state class lists only the moves that are legal from it, and the base class refuses all others with one message. Adding a state means adding one class. The Booking has no if on the status.
GoF lists the consequences. The pattern keeps behaviour for each state in one object, so new states and transitions are easy to add. It makes transitions explicit, and because the context changes state by rebinding one variable, the change is atomic and cannot leave the context half-way between states. States without instance variables can be shared, as the constants here are. The cost is more classes, and a less compact form than one class. GoF says that is a good trade when there are many states that would otherwise need large conditional statements.
State pattern
The State pattern represents each state of an object as its own class, so that the object delegates its behaviour to the class of its current state and changes state by switching which class it holds.
Example: Requested, Confirmed, CheckedIn and Cancelled.
Check yourself:
1. What does cancel() do on a booking in state CHECKED_IN?
2. Where does the "illegal move" message come from?
3. Why are the four state objects safe to share?