static void Main(string[] args) { Console.WriteLine("Observer Pattern example"); Console.WriteLine("------------------------"); var consoleClocks = new List<IConsoleClock>(); var clockSubject = new ClockSubject(); consoleClocks.Add(new GMTConsoleClock(clockSubject)); consoleClocks.Add(new SecondsCountingConsoleClock(clockSubject)); var isRunning = true; while (isRunning) { Console.WriteLine(); Console.WriteLine("Setting Time to now..."); clockSubject.SetState(new ClockState()); Console.WriteLine("Printing updated values from Clock Observers..."); foreach (var clock in consoleClocks) { clock.WriteStateToConsole(); } Console.WriteLine(); Console.WriteLine("X key to exit, any key to repeat."); var key = Console.ReadKey(); if (key.Key == ConsoleKey.X) { isRunning = false; } } }
public GMTConsoleClock(ClockSubject clockSubject) { // register with the clock subject for updates this.clockSubject = clockSubject; clockSubject.Attach(this); }
public SecondsCountingConsoleClock(ClockSubject clockSubject) { // register with the clock subject for updates this.clockSubject = clockSubject; clockSubject.Attach(this); }