Traffic Light System - Design Pattern

Problem Statement

Design a traffic signaling system or light system for a city. Which design pattern will you use for the basic traffic light system implementation?

Discussion

Traffic signaling system or light system deals with the conditions, given below:
  • Green Light- It represents the go or Move state of the vehicles. 
  • Yellow Light- It warns that signal is about to change to Red.
  • Red Light- It represents Stop state of the vehicles.
Thus, here object changes its behavior at run time without resorting to the large monolithic conditional statements. It allows an object to alter its behavior, when its internal state changes. Its state changes from Green, Yellow to Red and based on this, traffic behavior changes.
 
Thus, we found the points, given below, by finding out the change of state and behavior in traffic signaling systems.
  • Traffic signaling system's behavior depends on its states like Green, Yellow and Red and it is changing its behavior at the run-time, depending on the state.
  • It has multi-part conditional statements, which depends on traffic signal's state. 

Design Pattern Approach for Traffic Signaling or Light System

If you have read carefully above, you will find that discussion is only focused on the two points, given below.
  1. Change in state at the run-time.
  2. Change in behavior.
Thus, if I look into "Gang of four" Design pattern, I find that above two features are present in "State Design Pattern" under the category of "Behavioral Design Pattern", because it is dealing with the change of state and behavior.
 
Benefits of using State Design Pattern
 
The benefits are given below.
  1. You can get rid of switch statement.
  2. It provides flexibility because you don't know the future requirement changes.
Participants in the Design of Traffic Light System by using State Design Pattern
 
The classes, given below, Interface and objects will participate in the design of traffic light system.
  • ITrafficLight- It will define the methods to change the state and report state.
  • ConcreteState- It will be an implementation of ITrafficLight Interface.
  • TrafficLight- It will behave as a context for the traffic light or signal change.
Class Diagram
 
Class diagram, given below with state design pattern represents the complete flow of traffic light system.
 
 
Code Implementation
 
It defines the state of traffic signaling or traffic light system.
  1. using System;  
  2.   
  3. namespace StatePattern  
  4. {  
  5.     public interface ITrafficLight  
  6.     {  
  7.         void Change(TrafficLight light);  
  8.   
  9.         void ReportState();  
  10.     }  
  11. }  
Concrete state GreenLight is given below.
  1. using System;  
  2.   
  3. namespace StatePattern  
  4. {  
  5.    public class GreenLight : ITrafficLight  
  6.     {  
  7.         public void Change(TrafficLight light)  
  8.         {  
  9.             light.State = new YellowLight();  
  10.         }  
  11.   
  12.         public void ReportState()  
  13.         {  
  14.             Console.WriteLine("Green Light");  
  15.         }  
  16.     }  
  17. }  
Concrete state YellowLight is given below.
  1. using System;  
  2.   
  3. namespace StatePattern  
  4. {  
  5.     public class YellowLight : ITrafficLight  
  6.     {  
  7.         public void Change(TrafficLight light)  
  8.         {  
  9.             light.State = new GreenLight();  
  10.         }  
  11.   
  12.         public void ReportState()  
  13.         {  
  14.             Console.WriteLine("Yellow Light");  
  15.         }  
  16.     }  
  17. }  
Concrete state RedLight is given below.
  1. using System;  
  2.   
  3. namespace StatePattern  
  4. {  
  5.     public class RedLight :ITrafficLight  
  6.     {  
  7.         public void Change(TrafficLight light)  
  8.         {  
  9.             light.State = new GreenLight();     
  10.         }  
  11.   
  12.         public void ReportState()  
  13.         {  
  14.             Console.WriteLine("Red Light.");  
  15.         }  
  16.     }  
  17. }  
Context is given below.
  1. using System;  
  2.   
  3. namespace StatePattern  
  4. {  
  5.     public class TrafficLight  
  6.     {  
  7.         public ITrafficLight State { getset; }  
  8.   
  9.         public void Change()  
  10.         {  
  11.             State.Change(this);  
  12.         }  
  13.   
  14.         public void ReportState()  
  15.         {  
  16.             State.ReportState();  
  17.         }  
  18.     }  
  19. }  
Output
 
 
 
Find the attached zip code for the more detail. 

Conclusion

Thus, you should think about design during the development of any Application or module to make the Application flexible to extend. Discuss the case study and identify the behavior of an Application and you will find the design pattern under which Application will be designed. Here, you can see traffic signaling system has been designed by using state design pattern after identifying the behavior of an Application. It makes the Application flexible and maintainable.  
Next Recommended Reading Design Patterns in C#