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:
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:
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:
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
+----------------+
| Context |
|----------------|
| - state: State |
|----------------|
| + setState() |
| + handleInput()|
+----------------+
|
v
+----------------+
| State | <--- interface
+----------------+
^
|
+----------------+ +----------------+
| IdleState | | RunningState |
+----------------+ +----------------+How It Works
- The Context is created with an initial state.
- On a user action (
handleInput, and so on), the context calls the method of its current state. - The concrete state performs the needed logic and, if necessary, switches the context to another state.
- The context changes its
statereference, and from that point on behaves differently.
Conclusion
| Class | Role |
|---|---|
| Context | Holds the current state, delegates behavior to it |
| State | Defines the interface for all possible states |
| ConcreteState | Implements 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 readyA concise answer to help you respond confidently on this topic during an interview.