private static void Main(string[] args) { var weatherStation = new WeatherStation(); var tvDisplay = new TvDisplay(); var phoneDisplay = new PhoneDisplay(); weatherStation.AddObserver(tvDisplay); weatherStation.AddObserver(phoneDisplay); while (true) { weatherStation.Broadcast(string.Format("Hello display {0}", DateTime.UtcNow)); Thread.Sleep(1000); } }
static void Main(string[] args) { WeatherStation station = new WeatherStation(37); PhoneDisplay display1 = new PhoneDisplay(station); PhoneDisplay display2 = new PhoneDisplay(station); PhoneDisplay display3 = new PhoneDisplay(station); station.Add(display1); station.Add(display2); station.Add(display3); display1.Display(); display2.Display(); display3.Display(); station.Temperature = 40; display1.Display(); display2.Display(); display3.Display(); }
/// <summary> /// Definition: /// The observer pattern defines a one to many dependencies between objects so that when one object changes state, /// all of it's dependencies are notified and updated automatically. /// </summary> static void Main(string[] args) { WeatherStation station = new WeatherStation(); TabletDisplay tabletDisplay = new TabletDisplay(station); PhoneDisplay phoneDisplay = new PhoneDisplay(station); station.Add(tabletDisplay); station.Add(phoneDisplay); station.Notify(); station.Remove(tabletDisplay); station.Notify(); station.Add(tabletDisplay); station.Notify(); Console.WriteLine("\n--- Let's update the temperatur --- "); station.UpdateTemperatur(50); Console.ReadKey(); }