public static void Main()
var smsNotifier = new SmsNotifier();
var emailNotifier = new EmailNotifier();
abstract class AbstractState
public StateColors Color { get; protected set; }
public AbstractNotifier Notifier { get; set; }
class GreenState : AbstractState
private readonly AbstractNotifier _notifier;
public GreenState(AbstractNotifier notifier)
Color = StateColors.Green;
public GreenState(AbstractState state)
_notifier = state.Notifier;
Color = StateColors.Green;
public void SetColor(StateColors color)
if (color == StateColors.Red)
_notifier.State = new RedState(this);
class RedState : AbstractState
private readonly AbstractNotifier _notifier;
public RedState(AbstractState state)
_notifier = state.Notifier;
public void SetColor(StateColors color)
if (color == StateColors.Green)
_notifier.State = new GreenState(this);
abstract class AbstractNotifier
protected abstract bool Connect();
protected abstract void Send();
protected abstract void Disconnect();
public AbstractState State { get; set; }
public AbstractNotifier()
State = new GreenState(this);
Console.WriteLine($"Current state: {State.Color}");
State = new GreenState(State);
Console.WriteLine($"State: {State.Color}");
State = new RedState(State);
Console.WriteLine($"State: {State.Color}");
class SmsNotifier : AbstractNotifier
protected override bool Connect()
Console.WriteLine("Connecting to the SMS system");
protected override void Send()
Console.WriteLine("Sending SMS");
protected override void Disconnect()
Console.WriteLine("Disconnecting from the SMS system");
class EmailNotifier : AbstractNotifier
protected override bool Connect()
Console.WriteLine("Connecting to the Email system");
protected override void Send()
Console.WriteLine("Sending email");
protected override void Disconnect()
Console.WriteLine("Disconnecting from the Email system");