Click here to Skip to main content
12,821,041 members (33,591 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 26 Dec 2014

Illustrating the State Pattern with a Very Basic Example

, 29 Dec 2015 CPOL
Rate this:
Please Sign up or sign in to vote.
A simplified example is used to describe the state pattern


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.


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()
    public void 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(); //For the GreenLight class
 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();

The output is as expected:





Points of Interest

Using the state pattern is much easier to resolve and implement the problems which always changes their states.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rajesh Varma Buddaraju
United States United States
I am a Microsoft Certified Professional with 11 years of IT Professional experience with both Web and Windows Application using Microsoft Technologies/Tools such as ASP.NET using C#, VB.NET, ADO.NET, Entity Framework, Web Services, WCF Services, Restful Services, Java Script, XML, HTML, WPF, SSIS, SSRS, Silver light and Expression Blend, Entity Framework, Visual Basic 6.0 & Classic ASP.

You may also be interested in...

Comments and Discussions

QuestionCoupled too Tightly Pin
James Lonero4-Jan-16 7:08
memberJames Lonero4-Jan-16 7:08 
GeneralMy vote of 5 Pin
Member 43208442-Jan-16 23:59
memberMember 43208442-Jan-16 23:59 
QuestionIs this implementation correct? Pin
George Swan30-Dec-15 12:50
memberGeorge Swan30-Dec-15 12:50 
GeneralNice and easy to understand Pin
Salam Y. ELIAS20-Jan-15 1:53
memberSalam Y. ELIAS20-Jan-15 1:53 
Questionhave look at Pin
marc borgers31-Dec-14 1:24
membermarc borgers31-Dec-14 1:24 
GeneralMy vote of 5 Pin
N Srinivas 201329-Dec-14 3:51
memberN Srinivas 201329-Dec-14 3:51 
QuestionGreat Article Pin
Dimple Kumar Sharma26-Dec-14 5:05
memberDimple Kumar Sharma26-Dec-14 5:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 29 Dec 2015
Article Copyright 2014 by Rajesh Varma Buddaraju
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid