Skip to main content

What classes are involved in implementing the pattern?

Implementing the State pattern involves three main classes (or roles), that together provide a dynamic change of an object's behavior without conditional constructs.


1. Context

The object whose behavior depends on its state. It holds a reference to the current state object and delegates operations to it.

Context's tasks:

  • holds the current state (State currentState),
  • forwards calls to the state's methods,
  • can change its own state (via setState()).

Example:

java
class Player { private State state; public Player() { this.state = new IdleState(); } public void setState(State state) { this.state = state; } public void handleInput(String input) { state.handleInput(this, input); } }

2. State (State Interface)

Defines a common interface that all concrete states must implement. Each state describes the context's behavior in that particular state.

State's tasks:

  • declares the methods the context calls for specific actions,
  • sets the contract for all states (for example, handleInput(), update(), enter(), exit()).

Example:

java
interface State { void handleInput(Player context, String input); }

3. ConcreteState

Each subclass implements the behavior for a specific state. It can:

  • perform actions specific to that state;
  • switch the context to another state (via context.setState()).

Example:

java
class IdleState implements State { public void handleInput(Player context, String input) { if (input.equals("PRESS")) { System.out.println("Switching to the Running state"); context.setState(new RunningState()); } } } class RunningState implements State { public void handleInput(Player context, String input) { if (input.equals("STOP")) { System.out.println("Switching to the Idle state"); context.setState(new IdleState()); } } }

How They Interact

javascript
+----------------+ | Context | |----------------| | - state: State | |----------------| | + setState() | | + handleInput()| +----------------+ | v +----------------+ | State | <--- interface +----------------+ ^ | +----------------+ +----------------+ | IdleState | | RunningState | +----------------+ +----------------+

How It Works

  1. The Context is created with an initial state.
  2. On a user action (handleInput, and so on), the context calls the method of its current state.
  3. The concrete state performs the needed logic and, if necessary, switches the context to another state.
  4. The context changes its state reference, and from that point on behaves differently.

Conclusion

ClassRole
ContextHolds the current state, delegates behavior to it
StateDefines the interface for all possible states
ConcreteStateImplements specific behavior and manages transitions between states

Summary:

State works because the context delegates all of its current behavior to the states, and they can dynamically replace each other, changing the behavior without if-else.

Short Answer

Interview ready
Premium

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