Skip to main content

What does the State pattern do and when is it useful?

The State pattern is used to change an object's behavior when its internal state changes, without creating bulky if-else or switch constructs.


1. The Core Idea

Instead of an object deciding for itself what to do depending on its state, its current state is represented by a separate object, which knows how to behave in that particular context.

In other words: the object "delegates" its behavior to the current state. When the state changes, the behavior changes too.


2. The Basic Structure

  • Context - the object whose behavior depends on its state. Holds a reference to the current state object.
  • State (State Interface) - defines a common interface for all possible states.
  • ConcreteState (Concrete States) - implement the behavior for a specific state and can switch the context to another one.

3. Example (Java)

Example: an ATM that behaves differently depending on its state.

java
// State interface interface ATMState { void insertCard(ATM atm); void withdrawCash(ATM atm, int amount); } // Concrete states class NoCard implements ATMState { public void insertCard(ATM atm) { System.out.println("Card inserted"); atm.setState(new HasCard()); } public void withdrawCash(ATM atm, int amount) { System.out.println("No card - operation not possible"); } } class HasCard implements ATMState { public void insertCard(ATM atm) { System.out.println("Card is already inserted"); } public void withdrawCash(ATM atm, int amount) { System.out.println("Dispensed: " + amount); atm.setState(new NoCard()); } } // Context class ATM { private ATMState state = new NoCard(); public void setState(ATMState state) { this.state = state; } public void insertCard() { state.insertCard(this); } public void withdrawCash(int amount) { state.withdrawCash(this, amount); } }

Usage:

java
ATM atm = new ATM(); atm.withdrawCash(100); // No card - operation not possible atm.insertCard(); // Card inserted atm.withdrawCash(100); // Dispensed: 100

4. When It's Useful

1. When an object has several clearly defined states,

and its behavior must change depending on the state.

Example: an ATM (with a card / without a card), a game (player alive / dead), a document (draft / published).

2. When code with conditional operators became hard to maintain.

If methods keep containing:

java
if (state == OPEN) { ... } else if (state == CLOSED) { ... }

then State replaces this with state objects, making the code cleaner and more scalable.

3. When transitions between states happen dynamically,

and they need to be managed from inside the states themselves.

For example, on successful authorization, the "Guest" state moves the user into the "Authorized" state itself.


5. Advantages

  • Removes bulky if-else blocks.
  • Delegates responsibility for behavior to the states, code is easier to extend.
  • Behavior can be changed on the fly, by swapping the state object.
  • Makes it easier to add new states without changing the context.

6. Disadvantages

  • Increases the number of classes (each state is a separate class).
  • Can be excessive for overly simple scenarios.

Conclusion

The State pattern makes an object's behavior dynamic and clean, letting you change it "from the inside" without bulky conditions.

Summary:

State is a way to teach an object to behave differently depending on its state, making the code extensible, readable, and reactive, especially in places that used to be a solid block of if-else.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.