Skip to main content

🧩 State Pattern

✅ Intent

  • Change behavior based on the order status (unconfirmed / confirmed) or processing mode
  • Delegate behavior to state-specific objects, each representing a distinct state

✅ Motivation

  • The OrderManager was responsible for switching behavior based on state
  • Makes state transitions more explicit and safe

✅ When to Use

  • Step-based processes (e.g., wizard-style workflows)
  • Logic that varies based on the current state

✅ Code Example

interface OrderState {
confirm(order: StatefulOrderProcessor): void;
}

class UnconfirmedState implements OrderState {
confirm(order: StatefulOrderProcessor): void {
// 在庫調整・メール送信など
InventoryService.reduce(order.productIds);
EmailService.send(order.userEmail, "注文が確定されました");
OrderLogger.log("order-" + Math.random().toString(36).substring(2));

order.setState(new ConfirmedState());
}
}

class ConfirmedState implements OrderState {
confirm(order: StatefulOrderProcessor): void {
console.log("すでに確定済みです");
}
}

class StatefulOrderProcessor {
private state: OrderState;

constructor(public productIds: string[], public userEmail: string) {
this.state = new UnconfirmedState();
}

setState(state: OrderState) {
this.state = state;
}

confirmOrder() {
this.state.confirm(this);
}
}

// 利用例
const stateProcessor = new StatefulOrderProcessor(
["p01", "p02"],
"hiroshi@example.com"
);
stateProcessor.confirmOrder(); // 初回 → 確定
stateProcessor.confirmOrder(); // 2回目 → 確定済みメッセージ

✅ Explanation

This code applies the State pattern to switch behavior based on the current order status (unconfirmed or confirmed).
The State pattern encapsulates state-dependent behavior within separate classes, allowing the system to transition between states in a controlled manner.

1. Overview of the State Pattern

  • The following state classes are implemented based on the OrderState interface:
    • UnconfirmedState: Represents an order that has not yet been confirmed
    • ConfirmedState: Represents an already confirmed order

2. Key Classes and Their Roles

  • OrderState interface

    • Defines the interface for state-specific behavior
    • Requires implementation of the confirm(order: StatefulOrderProcessor): void method
  • UnconfirmedState

    • Represents the unconfirmed state of an order
    • The confirm method performs the following:
      • Reduces inventory
      • Sends a confirmation email to the user
      • Logs the order
      • Transitions the state to ConfirmedState
  • ConfirmedState

    • Represents a confirmed order
    • The confirm method outputs a message indicating the order is already confirmed
  • StatefulOrderProcessor

    • Context class that manages the current state
    • Holds the current state and delegates the confirmOrder method to the active state
    • State transitions are handled via the setState method

3. UML Class Diagram

4. Benefits of the State Pattern

  • Behavior is clearly separated by state: Each state's logic is self-contained, improving readability and maintainability
  • Explicit state transitions: Changes in state are clearly controlled through the setState method
  • Extensibility: New states can be added by implementing the OrderState interface

This design clearly separates state-dependent behavior and ensures safe and manageable state transitions.