100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

State Pattern

IntermediateTechnique5.5K learners

The State pattern is a behavioral object-oriented design pattern that lets an object alter its behavior when its internal state changes, by delegating state-specific behavior to separate state objects rather than using large conditional…

Definition

The State pattern is a behavioral object-oriented design pattern that lets an object alter its behavior when its internal state changes, by delegating state-specific behavior to separate state objects rather than using large conditional statements, making the object appear to change its class at runtime.

Overview

The State pattern is one of the classic Gang of Four (GoF) behavioral design patterns. It addresses a common problem: objects whose behavior depends heavily on an internal mode or state, which without the pattern tends to be implemented as large if/else or switch statements scattered across every method, checking the current state and branching accordingly. As the number of states and methods grows, this conditional logic becomes increasingly difficult to maintain and extend. The State pattern solves this by extracting each state into its own class implementing a common state interface, with the state-specific behavior for each method living inside its corresponding state class instead of inside conditional branches. The original 'context' object holds a reference to its current state object and delegates behavior-dependent method calls to it. Transitioning between states simply means swapping which state object the context currently references, either from within the state classes themselves or from the context. A canonical example is a traffic light or a media player: rather than a `Player` class checking `if (state == PLAYING)` throughout its methods, separate `PlayingState`, `PausedState`, and `StoppedState` classes each implement `play()`, `pause()`, and `stop()` with behavior appropriate to that state, including deciding what the next state should be. This makes adding a new state as simple as adding a new class, without touching existing state classes, aligning with the open/closed principle. The State pattern is closely related to the Strategy pattern structurally — both involve delegating behavior to an interchangeable object — but differs in intent: Strategy is about choosing an algorithm from the outside, while State is about an object's own internal, self-managed state transitions, often with the state objects themselves controlling when and how transitions occur.

Key Concepts

  • Encapsulates state-specific behavior in dedicated state classes
  • Context object delegates behavior to its current state object
  • Eliminates large conditional (if/switch) blocks checking internal state
  • State transitions occur by replacing the referenced state object
  • State classes can control their own transitions to other states
  • Adheres to the open/closed principle when adding new states
  • Structurally similar to, but conceptually distinct from, the Strategy pattern
  • One of the original 23 Gang of Four design patterns

Use Cases

Modeling UI components with multiple visual/behavioral modes (e.g., buttons, forms)
Implementing media players with play/pause/stop/buffering states
Building workflow or order-processing systems with sequential status transitions
Game character or enemy AI with distinct behavioral states
Network connection objects modeling connecting/connected/disconnected states
Traffic light or vending machine simulations
Parsers and protocol handlers implementing state machines

Frequently Asked Questions

From the Blog