State Pattern
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
Frequently Asked Questions
From the Blog
React Hooks Explained: useState, useEffect, and Beyond
React Hooks replaced class components and changed how React developers think about state and side effects. This guide explains useState, useEffect, useContext, useRef, and custom hooks clearly, with practical examples for each.
Read More Cloud & CybersecurityTerraform Basics: Infrastructure as Code on AWS
Terraform lets you define cloud infrastructure in code, version it in Git, and deploy it repeatably. This guide covers providers, resources, variables, outputs, state management, and real AWS examples — from a simple S3 bucket to a complete web server setup.
Read More Learn Through HobbiesLearn React Through Building a Gaming Leaderboard
Gaming leaderboards are the perfect React learning project: they need real-time state updates, list rendering, sorting, filtering, forms, and optional API fetching. This guide teaches core React through building a fully functional leaderboard for your favourite game.
Read More