State
Let an object change its behaviour when its internal state changes, with one class per state instead of status checks in every method.
Description
An order that is pending, paid, shipped or cancelled answers ship() very differently in each case. Instead of an if (status === …) ladder in every method, the State pattern gives each state its own class implementing the same interface. The context (the order) holds the current state object and delegates every action to it.
States also decide the transitions: Paid.ship() moves the order to Shipped. All the rules for one state sit in one small class, illegal actions get a clear answer, and adding a state means adding a class rather than editing every method.
Class diagram
State pattern class diagram
- Class Order with + pay(), + ship(), + cancel(), + setState(next).
- Interface OrderState with + pay(order), + ship(order), + cancel(order).
- Class Pending with + pay → Paid.
- Class Paid with + ship → Shipped.
- Class Shipped with + cancel: rejected.
- Class Cancelled with + all: rejected.
- Order current state OrderState.
- Pending implements OrderState.
- Paid implements OrderState.
- Shipped implements OrderState.
- Cancelled implements OrderState.
Sequence diagram
State pattern sequence diagram
- Client → Order: pay().
- Order → Pending: pay(order).
- Pending → Order: setState(new Paid()).
- Client receives ""payment received"" back from Order.
- Client → Order: ship().
- Order → Paid: ship(order).
- Paid → Order: setState(new Shipped()).
- Client receives ""handed to the courier"" back from Order.
TypeScript example
An order lifecycle with one class per state. Each state answers every action and decides the next state; the order just delegates.
// State: an order behaves differently depending on its lifecycle state,
// with one small class per state instead of status checks everywhere.
interface OrderState {
readonly name: string;
pay(order: Order): string;
ship(order: Order): string;
cancel(order: Order): string;
}
class Pending implements OrderState {
readonly name = "pending";
pay(order: Order): string {
order.setState(new Paid());
return "payment received";
}
ship(): string {
return "can't ship: not paid yet";
}
cancel(order: Order): string {
order.setState(new Cancelled());
return "cancelled, nothing to refund";
}
}
class Paid implements OrderState {
readonly name = "paid";
pay(): string {
return "already paid";
}
ship(order: Order): string {
order.setState(new Shipped());
return "handed to the courier";
}
cancel(order: Order): string {
order.setState(new Cancelled());
return "cancelled, refund issued";
}
}
class Shipped implements OrderState {
readonly name = "shipped";
pay(): string {
return "already paid";
}
ship(): string {
return "already on its way";
}
cancel(): string {
return "too late to cancel; start a return instead";
}
}
class Cancelled implements OrderState {
readonly name = "cancelled";
pay(): string {
return "order is cancelled";
}
ship(): string {
return "order is cancelled";
}
cancel(): string {
return "already cancelled";
}
}
// Context: delegates every action to its current state
class Order {
private state: OrderState = new Pending();
setState(next: OrderState): void {
this.state = next;
}
get status(): string {
return this.state.name;
}
pay(): string {
return this.state.pay(this);
}
ship(): string {
return this.state.ship(this);
}
cancel(): string {
return this.state.cancel(this);
}
}
export function demo(): string[] {
const order = new Order();
const steps: Array<() => string> = [() => order.ship(), () => order.pay(), () => order.ship(), () => order.cancel()];
return steps.map((step) => {
const before = order.status;
const result = step();
return `${before} → ${result} → ${order.status}`;
});
}When to use it
Use it when
- An object's behaviour depends on a status, and the same status checks repeat across many methods.
- There are clear lifecycle states and transitions: orders, documents in review, connections, subscriptions.
- Illegal actions in a state should be impossible or explicitly rejected, not silently allowed.
Avoid it when
- There are two states and one or two behaviours that differ. A boolean and an
ifare clearer. - You mainly need to validate transitions, not change behaviour. A transition table (state × event → next state) is lighter.
- The lifecycle is long-running and spans services or days. Use a workflow engine or persisted state machine.
Pitfalls
- Storing the state object but persisting only a status string. Make sure you can rebuild the state object from what's saved.
- States that reach into the context's internals, which couples every state to every field. Give the context a small API for states.
- Transitions spread across many states make the overall lifecycle hard to see. Document it with a state diagram, or centralise transitions.
Alternatives
Strategy
Choose it when the caller picks the behaviour from outside; the object doesn't switch on its own.
Discriminated union + switch
Choose it when in TypeScript, a status union with exhaustive switch statements is often enough, and the compiler checks every case.
State machine library
Choose it when you have many states, guards and nested states; XState and similar tools make the machine explicit and visualisable.