For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt, and this page is available as Markdown at https://nhanbe.id.vn/design-patterns/behavioral/state.md.

Allow an object to alter its behavior when its internal state changes. Behavior varies based on state. You want to avoid large conditionals. Pros:

State

Overview

Allow an object to alter its behavior when its internal state changes.

When to use

  • Behavior varies based on state.
  • You want to avoid large conditionals.

Java example

interface State {
    String render();
}

class DraftState implements State {
    public String render() { return "draft"; }
}

class Document {
    private State state = new DraftState();
    void setState(State state) { this.state = state; }
    String render() { return state.render(); }
}

TypeScript example

interface State {
  render(): string;
}

class DraftState implements State {
  render(): string { return "draft"; }
}

class Document {
  private state: State = new DraftState();
  setState(state: State): void { this.state = state; }
  render(): string { return this.state.render(); }
}

Pros and cons

Pros:

  • Removes complex conditional logic.
  • State transitions are explicit.

Cons:

  • More classes for each state.
  • Can be overkill for simple cases.

Common pitfalls

  • States that leak knowledge of others.
  • Hidden transitions spread across code.