Software engineering / State pattern

State pattern

An order must react differently to the same request when it is new, paid or shipped. We replace the switch statements with one small class per state and watch the order move between them.

Intermediate7 steps~7 min
Order — the context: holds one current state
State — a class with the behaviour for one situation
Current state — the one the order is in right now
Call — a request such as pay, ship or cancel
// THE SHORT VERSION

In 30 seconds

Some objects behave differently depending on their situation: an order can be cancelled freely when new, needs a refund when paid, and cannot be cancelled once shipped.

The state pattern gives each situation, or state, its own small class containing the behaviour for that state. The object keeps one of them as its current state and passes every request to it.

A state can replace itself with another: a transition. The states and the allowed transitions together form a state machine, which replaces large switch statements spread through the code.

State pattern, step by step

STEPS01 / 07

Simplified: the pattern adds classes, so for two or three states and a couple of methods a switch or a transition table is often clearer. Adding a new method still touches the state classes that react to it.

STEP 01 / 07 Same call, different result
THE MATH
new: cancel paid: refund, cancel shipped: refuse
  1. Step 1: Same call, different result. A shop’s order can be asked to cancel at any moment, and the right response depends on where the order is. A new order is simply cancelled. A paid one must refund the €40 first. A shipped one has to refuse. The order’s situation is called its state, and its behaviour depends on it.
  2. Step 2: The switch that grows. The usual first version stores the state as a word and lets every method look at it with a switch. With three methods and four states that is twelve cases, and the rules for one state are scattered over all the methods. A fifth state means opening each method and adding a case; forget one and that state misbehaves.
  3. Step 3: One class per state. Cut the same twelve cases the other way. Each state becomes a small class of its own, and that class holds its own version of every method: what pay, ship and cancel mean while the order is in that state. Everything about “paid” is now in one place and can be read, tested and changed alone.
  4. Step 4: The order delegates. The order keeps one state object in a slot: its current state. When someone calls cancel, the order does not decide anything. It passes the call to whatever object is in the slot, and that object’s version of cancel runs. The switch is gone; picking the right behaviour is done by which object is plugged in.
  5. Step 5: States cause transitions. PaidState’s cancel does the work for its situation: it refunds the €40, then puts a CancelledState object into the order’s slot. That replacement is a transition. From now on the very same order answers every call the way a cancelled order should, although no code in the order itself has changed.
  6. Step 6: The state machine. Draw every state as a box and every transition as an arrow labelled with the call that causes it, and you have a state machine: a fixed set of states and the allowed moves between them. An order starts as new and can only travel along the arrows. A call with no arrow leaving the current state is refused.
  7. Step 7: Adding a state. Customers may now send a shipped order back. That is one new class, ReturnedState, and one new method on ShippedState that leads to it. The other states refuse the new call through a shared default, so their code stays as it was. With switches, every existing method would have needed another case.
VALUES SO FAR
callcancel()request
outcomes3 behavioursby state
cases····switch
classes····rule
current····slot
refund····action
current····moved
arrows····machine
added····new

Spotted a mistake on this page?