Simple definition#
The State pattern moves state-specific behaviour into separate state objects. The main object (the context) delegates each request to its current state object, and the state object decides what to do and which state comes next. To the outside, the object appears to change its class.
GoF intent: “Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.”
The problem#
An order can be awaiting payment, paid, shipped, delivered or cancelled. Every operation behaves differently in each state:
void cancel() {
switch (status) {
case AWAITING_PAYMENT -> status = CANCELLED;
case PAID -> { refund(); status = CANCELLED; }
case SHIPPED, DELIVERED -> throw new IllegalStateException("too late");
case CANCELLED -> throw new IllegalStateException("already cancelled");
}
}
pay(), ship() and deliver() contain similar switch statements. The rules for one state are scattered across every method; adding a state (“on hold”) means editing all of them, and it is easy to forget one.
Real-world analogy#
A traffic light. Pressing the pedestrian button means different things when the light is green (start the change cycle), amber (do nothing) or red (nothing; already walking). The behaviour belongs to the state, not the button.
Structure#
- Context (
Order) — holds the current state and delegates to it. - State (
OrderState) — one method per event. - Concrete states (
AwaitingPayment,Paid,Shipped,Delivered,Cancelled) — implement the behaviour and return the next state.
stateDiagram-v2
[*] --> AwaitingPayment
AwaitingPayment --> Paid : pay
AwaitingPayment --> Cancelled : cancel
Paid --> Shipped : ship
Paid --> Cancelled : cancel / refund
Shipped --> Delivered : deliver
Delivered --> [*]
Cancelled --> [*]classDiagram
direction LR
class Order {
-OrderState state
+pay()
+ship()
+deliver()
+cancel()
}
class OrderState {
<<interface>>
+pay(Order) OrderState
+ship(Order) OrderState
+deliver(Order) OrderState
+cancel(Order) OrderState
}
class AwaitingPayment
class Paid
class Shipped
class Delivered
class Cancelled
Order o--> OrderState : current
OrderState <|.. AwaitingPayment
OrderState <|.. Paid
OrderState <|.. Shipped
OrderState <|.. Delivered
OrderState <|.. CancelledJava implementation#
The code is in examples/java/src/hub/patterns/state/StateDemo.java.
/** State: one method per event. Each concrete state decides what the event means in that state. */
interface OrderState {
default OrderState pay(Order order) { throw invalid("pay"); }
default OrderState ship(Order order) { throw invalid("ship"); }
default OrderState deliver(Order order) { throw invalid("deliver"); }
default OrderState cancel(Order order) { throw invalid("cancel"); }
String name();
private IllegalStateException invalid(String event) {
return new IllegalStateException("cannot " + event + " an order that is " + name());
}
}final class AwaitingPayment implements OrderState {
@Override public OrderState pay(Order o) { o.log("payment captured"); return new Paid(); }
@Override public OrderState cancel(Order o) { o.log("cancelled before payment"); return new Cancelled(); }
@Override public String name() { return "AWAITING_PAYMENT"; }
}
final class Paid implements OrderState {
@Override public OrderState ship(Order o) { o.log("handed to carrier"); return new Shipped(); }
@Override public OrderState cancel(Order o) { o.log("cancelled, refund issued"); return new Cancelled(); }
@Override public String name() { return "PAID"; }
}
final class Shipped implements OrderState {
@Override public OrderState deliver(Order o) { o.log("delivered"); return new Delivered(); }
@Override public String name() { return "SHIPPED"; } // cancel is not allowed: inherited default throws
}
final class Delivered implements OrderState {
@Override public String name() { return "DELIVERED"; }
}
final class Cancelled implements OrderState {
@Override public String name() { return "CANCELLED"; }
}/** Context: delegates every event to its current state object and stores the returned next state. */
final class Order {
private final String id;
private OrderState state = new AwaitingPayment();
Order(String id) { this.id = id; }
void pay() { state = state.pay(this); }
void ship() { state = state.ship(this); }
void deliver() { state = state.deliver(this); }
void cancel() { state = state.cancel(this); }
String status() { return state.name(); }
void log(String message) { System.out.println(" " + id + ": " + message); }
} ORD-1: payment captured
ORD-1: handed to carrier
ORD-1: delivered
ORD-1 is DELIVERED
ORD-2: payment captured
ORD-2: handed to carrier
ORD-2: rejected - cannot cancel an order that is SHIPPED
ORD-2 is SHIPPED
ORD-3: cancelled before payment
ORD-3 is CANCELLEDStep-by-step walkthrough#
- Default methods reject everything. Each state overrides only the events that are valid in it. An invalid transition (cancelling a shipped order) throws a clear exception without any code in
Shipped. - States return the next state. The transition table is distributed across the state classes, next to the behaviour it governs.
- The context stores the result.
state = state.cancel(this)keeps transitions explicit and makes states stateless, so they could be shared singletons. - Adding a state (“OnHold”) means one new class and changes only in the states that can transition to it.
Alternatives in modern Java#
Enum with constant-specific bodies — compact when states have no data:
enum Status {
AWAITING_PAYMENT { Status pay() { return PAID; } Status cancel() { return CANCELLED; } },
PAID { Status ship() { return SHIPPED; } Status cancel() { return CANCELLED; } },
SHIPPED { Status deliver() { return DELIVERED; } },
DELIVERED, CANCELLED;
Status pay() { throw new IllegalStateException(name()); }
Status ship() { throw new IllegalStateException(name()); }
Status deliver() { throw new IllegalStateException(name()); }
Status cancel() { throw new IllegalStateException(name()); }
}
Sealed records with state-specific data — when each state carries different fields (Shipped(trackingNumber), Cancelled(reason)), a sealed interface plus switch pattern matching lets the compiler check that every state is handled.
A transition table (Map<Status, Map<Event, Status>>) or a library such as Spring Statemachine — when transitions are configured rather than coded, or when you need guards, actions and persistence of state machines.
State versus Strategy#
The class diagrams are identical; the difference is who changes the object and why:
| State | Strategy | |
|---|---|---|
| Who chooses the object | The states themselves, as transitions | The client, usually once |
| Do the objects know each other | Yes, they return the next state | No, strategies are independent |
| What it models | A lifecycle with rules | Interchangeable algorithms |
See Strategy vs State vs Template Method.
Advantages and disadvantages#
| Advantages | Disadvantages |
|---|---|
| State-specific behaviour is localised in one class per state | More classes than a switch for simple machines |
| Invalid transitions are impossible or explicit | The full transition table is spread across classes; draw the diagram |
| Adding states touches fewer places | States are coupled to each other (they create successors) |
| Removes large conditional blocks | — |
When to use it#
- An object’s behaviour depends on its state and it must change behaviour at runtime.
- Operations have large, multi-part conditionals on the same status field.
- Domain lifecycles: orders, payments, documents, tickets, connections.
When not to use it#
- Two or three states with trivial behaviour: an enum and a
switchare clearer. - The state machine is configured at runtime: use a table-driven approach or a workflow engine.
Common mistakes#
- Persisting the state object instead of a status value. Store an enum or string and map it back to a state object when loading.
- Forgetting concurrency: two requests transitioning the same order concurrently need optimistic locking (
@Version) in the database. - Silent no-ops for invalid events, which hide bugs. Prefer explicit rejection.
Real-world uses#
- TCP connection states (the GoF book’s own example).
- Order, payment and subscription lifecycles in DDD aggregates; see DDD fundamentals.
java.lang.Thread.State(an enum of states), JSF and Spring Web Flow lifecycles, Spring Statemachine.- The circuit breaker is a state machine: CLOSED, OPEN, HALF_OPEN.
Interview questions and answers#
What is the State pattern? A behavioral pattern in which an object delegates behaviour to a state object and changes that object when its state changes, so its behaviour changes without conditionals.
State versus Strategy? Same structure. States know about each other and drive transitions themselves; strategies are independent and chosen by the client.
How would you implement a state machine in Java 21?
For simple machines, an enum with constant-specific methods; for states with data, a sealed interface of records with pattern-matching switch; for configurable machines, a transition table or Spring Statemachine.
How do you persist an object that uses the State pattern? Persist a status value (enum), and rebuild the state object on load. Protect concurrent transitions with optimistic locking.
Summary#
- State replaces status
switchstatements with polymorphic state objects. - Each state handles valid events and returns the next state.
- Enums and sealed records are lightweight alternatives in Java 21.
- Draw the state diagram; it is the specification.
Related topics#
- Strategy vs State vs Template Method
- Strategy Pattern
- Domain-Driven Design Fundamentals
- Circuit Breakers and Resilience4j
References#
- Gamma, Helm, Johnson, Vlissides. Design Patterns. Addison-Wesley, 1994. State, p. 305.
- Eric Freeman, Elisabeth Robson. Head First Design Patterns (2nd edition). O’Reilly, 2020. Chapter 10 (the gumball machine).