Introduction
The state pattern is a very much used design pattern and it allows a context to change its behavior as the state of the context changes.
Problem
For example, we have a real time scenario. In a traffic signal, the state of the signal changes all the time. The red signal which indicates to stop, the green which indicates to go and so on. In this situation, the state of the signal changes from one state to another. If we implement this scenario in the traditional approach, then it is very clumsy and a maintenance nightmare. We can resolve these kind of problems using state pattern.
Using the Code
In the discussed problem, there are two functionalities; one is change the state and one is report the state. So an interface is required to declare these three methods. Let's take ITrafficLight
as interface here.
Change State: Which means the signal changes from one state to another.
Report State: What is the signal state after changes its state means ("Red
", "Yellow
" or "Green
").
public interface ITrafficLight
{
void change(TrafficLight light);
void ReportState();
}
A class TrafficLight
is required to hold the interface property and set the current state and display the changed state.
public class TrafficLight
{
public ITrafficLight state { get; set; }
public void change()
{
state.change(this);
}
public void Reportstate()
{
state.ReportState();
}
}
Three classes are required to implement the interface methods:
GreenLight
RedLight
YelloLight
All these three classes implement the ITrafficLight
interface which means the classes need to specify the changeState
and the ReportState
. The interesting thing here is we will always pass the next state to the current class. For suppose after the green light indication, the yellow light follows and the red light follows after yellow. So why we are passing the "YelloLight
" instance in GreenLight
class and so on.
light.state = new YelloLight();
public class GreenLight:ITrafficLight
{
public void change(TrafficLight light)
{
light.state = new YelloLight();
}
public void ReportState()
{
Console.WriteLine("Green light");
}
}
public class RedLight:ITrafficLight
{
public void change(TrafficLight light)
{
light.state = new GreenLight();
}
public void ReportState()
{
Console.WriteLine("Red Light");
}
}
public class YelloLight:ITrafficLight
{
public void change(TrafficLight light)
{
light.state = new RedLight();
}
public void ReportState()
{
Console.WriteLine("Yellow Light");
}
}
Finally, take an object for the TrafficLight
class and inform the state and keep on changing the state. So it will automatically follow the state and changes its state.
TrafficLight light = new TrafficLight();
light.state = new RedLight();
while(true)
{
light.change();
light.Reportstate();
}
Console.ReadKey();
The output is as expected:
RedLight
GreenLight
YellowLight
.
Points of Interest
Using the state pattern is much easier to resolve and implement the problems which always changes their states.