We have the IState
interface, which defines the contract for different states. The StateA
, StateB
, and StateC
classes are concrete implementations of the state interface.
The Context
class represents the object that has different states. It maintains a reference to the current state and delegates the handling of requests to the current state by calling its Handle
method. The Context
class also provides a method called Request
to initiate a request.
In the Main
method, we create an instance of the Context
class. We then call the Request
method on the context multiple times, which triggers the handling of the request by the current state. The output displays the messages indicating which state is currently handling the context.
The State Pattern allows an object to alter its behavior when its internal state changes. It encapsulates each state as a separate class, and the object's behavior varies based on its internal state. This pattern promotes loose coupling between the client and the states, as the client interacts with the context object without being aware of the specific state classes.
using System;
// Step 1: Define the state interface
public interface IState
{
void Handle(Context context);
}
// Step 2: Implement concrete states
public class StateA : IState
{
public void Handle(Context context)
{
Console.WriteLine("State A is handling the context.");
// Perform state-specific operations
// Transition to the next state if needed
context.State = new StateB();
}
}
public class StateB : IState
{
public void Handle(Context context)
{
Console.WriteLine("State B is handling the context.");
// Perform state-specific operations
// Transition to the next state if needed
context.State = new StateC();
}
}
public class StateC : IState
{
public void Handle(Context context)
{
Console.WriteLine("State C is handling the context.");
// Perform state-specific operations
// Transition to the next state if needed
context.State = new StateA();
}
}
// Step 3: Define the context
public class Context
{
public IState State { get; set; }
public Context()
{
// Set the initial state
State = new StateA();
}
public void Request()
{
// Delegate the handling to the current state
State.Handle(this);
}
}
// Step 4: Usage example
class Program
{
static void Main(string[] args)
{
// Create the context
var context = new Context();
// Perform requests on the context
context.Request(); // State A handles the context
context.Request(); // State B handles the context
context.Request(); // State C handles the context
context.Request(); // State A handles the context
}
}