LLDintermediate

State Pattern

The State Pattern models behavior that changes based on an object's current state. It is especially effective for lifecycle-heavy systems such as orders, bookings, approvals, and workflows.

Reading time

9 min

statepatternsworkflowlifecycle

What Problem It Solves

When code branches heavily based on status values, it becomes hard to maintain and extend. State objects encapsulate state-specific behavior and transitions.

Common Examples

  • Order lifecycle
  • Vending machine states
  • Document review workflow
  • Ticket or booking lifecycle

Benefits

  • Cleaner transitions
  • Easier testing
  • More explicit behavior mapping

Interview Tip

For systems with statuses and transitions, proactively mention state pattern instead of a giant switch.