Ejemplo n.º 1
0
        public static void Run()
        {
            WeatherDataWithEvent data = new WeatherDataWithEvent(13);

            CurrentConditionDisplayWithEvent display1 = new CurrentConditionDisplayWithEvent("Display1", data);
            // OUTPUT --> Display1 - Temperature: 13
            CurrentConditionDisplayWithEvent display2 = new CurrentConditionDisplayWithEvent("Display2", data);

            // OUTPUT --> Display2 - Temperature: 13

            // update the temperature.
            data.Temperature = 14;

            // Display 1 and 2 show the new temperature
            // OUTPUT:

            /* Weather data is updated
             * Display1 - Temperature: 14
             * Weather data is updated
             * Display2 - Temperature: 14
             */

            data.OnTemperatureChanged -= display2.OnTemperatureChanged;
            data.Temperature           = 15;

            // Only Display 1 shows the new temperature
            // OUTPUT:

            /* Weather data is updated
             * Display1 - Temperature: 15
             */

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public CurrentConditionDisplayWithEvent(string name, WeatherDataWithEvent data)
        {
            this.name = name;
            this.data = data;

            this.data.OnTemperatureChanged += this.OnTemperatureChanged;
            Display();
        }
Ejemplo n.º 3
0
 public void OnTemperatureChanged(WeatherDataWithEvent data)
 {
     Console.WriteLine("Weather data is updated");
     this.data = data;
     Display();
 }