State pattern | Behavioral design pattern
State pattern
The state pattern is a behavioral design pattern that allows an object to alter its behavior when its internal state changes.
Key components of the state pattern include:
1. Context:
- The context is the object that contains the state. It delegates the state-specific behavior to different state objects.
- It can have methods that change its internal state and, consequently, its behavior.
2. State:
- The state interface defines a set of methods that concrete state classes must implement.
- Each concrete state class represents a specific state and provides its own implementation of the state interface methods.
3. Concrete State:
- Concrete state classes implement the state interface and define the behavior associated with a particular state of the context.
- The context delegates state-specific tasks to the current concrete state object.
4. Context-Specific Behavior:
- The behavior of the context varies based on its internal state.
- Context-specific behavior is implemented by delegating tasks to the current state object.
5. State Transition:
- State transitions occur when the context changes its internal state.
- The context switches from one concrete state to another, affecting its behavior accordingly.
By using the state pattern, you can achieve a clean separation of concerns between different states, making it easier to add new states or modify existing ones without affecting the context's code.
C# example. Suppose we are modeling a traffic light with three states: Red, Yellow, and Green.
using System;
// State interface
public interface ITrafficLightState
{
void Handle(TrafficLight context);
}
// Concrete states
public class RedState : ITrafficLightState
{
public void Handle(TrafficLight context)
{
Console.WriteLine("Traffic Light is RED. Stop!");
// Perform actions specific to the Red state
}
}
public class YellowState : ITrafficLightState
{
public void Handle(TrafficLight context)
{
Console.WriteLine("Traffic Light is YELLOW. Prepare to stop or go!");
// Perform actions specific to the Yellow state
}
}
public class GreenState : ITrafficLightState
{
public void Handle(TrafficLight context)
{
Console.WriteLine("Traffic Light is GREEN. Go!");
// Perform actions specific to the Green state
}
}
// Context
public class TrafficLight
{
private ITrafficLightState currentState;
public TrafficLight()
{
// Initialize with the default state (e.g., Red)
currentState = new RedState();
}
public void ChangeState(ITrafficLightState newState)
{
currentState = newState;
}
public void Request()
{
// Delegate the request to the current state
currentState.Handle(this);
}
}
// Example usage
class Program
{
static void Main()
{
TrafficLight trafficLight = new TrafficLight();
// Initial state
trafficLight.Request(); // Output: Traffic Light is RED. Stop!
// Change to Yellow state
trafficLight.ChangeState(new YellowState());
trafficLight.Request(); // Output: Traffic Light is YELLOW. Prepare to stop or go!
// Change to Green state
trafficLight.ChangeState(new GreenState());
trafficLight.Request(); // Output: Traffic Light is GREEN. Go!
}
}
In this example, the `TrafficLight` class has a current state, and based on that state, it delegates the handling of requests to different concrete state classes (`RedState`, `YellowState`, `GreenState`). Changing the state allows the traffic light to exhibit different behaviors.
Comments
Post a Comment