Mid From PDF GoF Patterns Gang of Four Patterns

Concrete States:?

Short answer: Each state (Red, Green, Yellow) is represented by a concrete class that implements ITrafficLightState. These classes define how the traffic light behaves in each state and how it transitions to the next state. RedState: When the light is red, the action is "stop," and the state transitions to GreenState. public class RedState : ITrafficLightState

Example code

{
public void Change(TrafficLight light)
{ Console.WriteLine("Red light - stop."); light.SetState(new GreenState()); }
} ● GreenState: When the light is green, the action is "go," and the state transitions to YellowState. public class GreenState : ITrafficLightState
{
public void Change(TrafficLight light)
{ Console.WriteLine("Green light - go."); light.SetState(new YellowState()); }
} ● YellowState: When the light is yellow, the action is "caution," and the state transitions to RedState. public class YellowState : ITrafficLightState
{
public void Change(TrafficLight light)
{ Console.WriteLine("Yellow light - caution."); light.SetState(new RedState()); }
}

Real-world example (ShopNest)

Use a GoF pattern in ShopNest only when it removes duplication or makes a change safer—explain the problem it solves in the interview.

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details