Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var weatherStation = new WeatherStation();

            var newsChannel1 = new NewsChannel("Independence TV");

            weatherStation.AddObserver(newsChannel1);

            var newsChannel2 = new NewsChannel("India Tomorrow");

            weatherStation.AddObserver(newsChannel2);

            weatherStation.Temperature = 10.2f;
            weatherStation.Temperature = 23.2f;
            weatherStation.Temperature = 45.2f;
        }
        static void Main(string[] args)
        {
            // Create weather station - Observable
            WeatherStation weatherStation = new WeatherStation();

            // Create Displays - Observers
            TemperatureDisplay temperatureDisplay = new TemperatureDisplay();
            WindDisplay        windDisplay        = new WindDisplay();

            // Add Observers to Observable
            weatherStation.Add(temperatureDisplay);
            weatherStation.Add(windDisplay);

            // To demonstrate, manually triiger the Notify() of the observable
            weatherStation.Notify();
        }
Esempio n. 4
0
        private static void ObserverDesignPattern()
        {
            Console.WriteLine("Observer Pattern Example:");
            Console.WriteLine("-------------------------------------------------------------------");
            var weatherStation = new WeatherStation();

            var currentConditionsDisplay = new CurrentConditionsDisplay();
            var staticsDisplay = new StaticsDisplay();

            weatherStation.registerObserver(currentConditionsDisplay);
            weatherStation.registerObserver(staticsDisplay);

            weatherStation.setMeasurements(12, 23, 45);

            Console.WriteLine("-------------------------------------------------------------------");
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            WeatherStation weatherStation = new WeatherStation();
            NewsAgency     agency1        = new NewsAgency("Alpha");
            NewsAgency     agency2        = new NewsAgency("Beta");
            NewsAgency     agency3        = new NewsAgency("Gamma");
            NewsAgency     agency4        = new NewsAgency("Delta");

            weatherStation.Attach(agency1);
            weatherStation.Attach(agency2);
            weatherStation.Attach(agency3);
            weatherStation.Attach(agency4);
            weatherStation.Temperature = 22.1f;
            weatherStation.Temperature = 37.5f;
            weatherStation.Temperature = 49.4f;
            weatherStation.Temperature = 0.4f;
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            IWeatherObservable station             = new WeatherStation();
            StandardDisplay    weatherDisplay      = new StandardDisplay(station);
            FancyDisplay       fancyWeatherDisplay = new FancyDisplay(station);

            station.SetMeasurments(new WeatherData()
            {
                Humidity = 69, Pressure = 90, RainQuantity = 46, Temperature = 28
            });
            station.Unsubscribe(fancyWeatherDisplay);
            Console.WriteLine("Fancy display unsubscribed");

            station.SetMeasurments(new WeatherData()
            {
                Humidity = 60, Pressure = 99, RainQuantity = 11, Temperature = 26
            });
            fancyWeatherDisplay.GetWeatherData();
        }
        /// <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();
        }
Esempio n. 8
0
        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();
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            WeatherStation weatherStation = new WeatherStation();

            NewsAgency agency1 = new NewsAgency("Best News Agency");

            weatherStation.Attach(agency1);
            NewsAgency agency2 = new NewsAgency("Fake News Agency");

            weatherStation.Attach(agency2);
            NewsAgency agency3 = new NewsAgency("Yellow Press Agency");

            weatherStation.Attach(agency3);

            weatherStation.Temp = 18.4f;
            weatherStation.Temp = 25.6f;
            weatherStation.Temp = 31.4f;
            weatherStation.Temp = 14.4f;
            weatherStation.Temp = 29.6f;

            Console.ReadKey();
        }
        public SmartDevice(IObservable publisher)
        {
            var weatherStation = (WeatherStation)publisher;

            _weatherStation = weatherStation;
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            var stLouisWeatherStation    = new WeatherStation();
            var kansasCityWeatherStation = new WeatherStation();

            var myPhone    = new PhoneApp(stLouisWeatherStation);
            var yourPhone  = new PhoneApp(kansasCityWeatherStation);
            var myDevice   = new SmartDevice(stLouisWeatherStation);
            var yourDevice = new SmartDevice(kansasCityWeatherStation);

            Console.WriteLine("My Phone:");
            myPhone.Display();
            Console.WriteLine("Your Phone:");
            yourPhone.Display();
            Console.WriteLine("My Device:");
            myDevice.Display();
            Console.WriteLine("Your Device:");
            yourDevice.Display();

            Console.WriteLine("Register subscribers");
            stLouisWeatherStation.Register(myPhone);
            stLouisWeatherStation.Register(myDevice);
            kansasCityWeatherStation.Register(yourPhone);
            kansasCityWeatherStation.Register(yourDevice);

            Console.WriteLine("My Phone:");
            myPhone.Display();
            Console.WriteLine("Your Phone:");
            yourPhone.Display();
            Console.WriteLine("My Device:");
            myDevice.Display();
            Console.WriteLine("Your Device:");
            yourDevice.Display();

            Console.WriteLine("Temperature in St. Louis changes to 72.3");
            Console.WriteLine("Humidity in St. Louis changes to 42.1");
            stLouisWeatherStation.Temperature = 72.3M;
            stLouisWeatherStation.Humidity    = 42.1M;

            Console.WriteLine("My Phone:");
            myPhone.Display();
            Console.WriteLine("Your Phone:");
            yourPhone.Display();
            Console.WriteLine("My Device:");
            myDevice.Display();
            Console.WriteLine("Your Device:");
            yourDevice.Display();

            Console.WriteLine("Temperature in Kansas City changes to 73.8");
            Console.WriteLine("Humidity in Kansas City changes to 41.3");
            kansasCityWeatherStation.Temperature = 73.8M;
            kansasCityWeatherStation.Humidity    = 41.3M;

            Console.WriteLine("My Phone:");
            myPhone.Display();
            Console.WriteLine("Your Phone:");
            yourPhone.Display();
            Console.WriteLine("My Device:");
            myDevice.Display();
            Console.WriteLine("Your Device:");
            yourDevice.Display();

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Esempio n. 12
0
 public Phone(WeatherStation weatherStation)
 {
     _weatherStation = weatherStation;
 }
Esempio n. 13
0
        public void GetWeatherData()
        {
            WeatherData data = WeatherStation.GetMeasurments();

            Console.WriteLine("Requested manually: temperature: {0}, humidity: {1}, pressure {2}", data.Temperature, data.Humidity, data.Pressure);
        }
Esempio n. 14
0
 public PhoneDisplay(WeatherStation weatherStation)
 {
     this.weatherStation = weatherStation;
 }
Esempio n. 15
0
        public PhoneApp(IObservable publisher)
        {
            var weatherStation = (WeatherStation)publisher;

            _weatherStation = weatherStation;
        }
Esempio n. 16
0
 public Form1()
 {
     InitializeComponent();
     station = new WeatherStation();
     comboBox1.DataSource = Enum.GetValues(typeof(Weather));
 }
Esempio n. 17
0
 public Television(WeatherStation weatherStation)
 {
     _weatherStation = weatherStation;
 }
Esempio n. 18
0
 public MobileDevice(WeatherStation station)
 {
     _weatherStation = station;
 }
 public TabletDisplay(WeatherStation weatherStation)
 {
     this.weatherStation = weatherStation;
 }