7
← Back to Pattern Arsenal
behavioral

State

must know

Stage 1: Problem

Problem

You are building an order management system. An order moves through these statuses: Draft → PendingPayment → Paid → Processing → Shipped → Delivered. It can also be Cancelled (from Draft, PendingPayment, or Paid) or Refunded (only from Delivered, within 30 days).

Rules:

  • In Draft, the customer can edit items and submit for payment.
  • In PendingPayment, only pay or cancel is allowed.
  • In Paid, the warehouse can start processing. The customer can still cancel (triggers a refund).
  • In Processing, the order cannot be cancelled. Once packed, it moves to Shipped.
  • In Shipped, only delivery confirmation is allowed.
  • In Delivered, the customer can request a refund within 30 days.

Each status has different allowed actions, different validation rules, and different side effects (emails, inventory locks, payment captures).

Now the business asks:

  1. Add a new status OnHold — an admin can pause any order between Paid and Shipped. On hold, no actions are allowed except resume or cancel.
  2. Add a ReturnRequested status after Delivered.

How would you structure the code so adding a new status doesn't require editing every method?


Related Patterns

Confused With a Similar Pattern?

1 / 10 — Problem