Ejemplo n.º 1
0
        public void WhenCreateDisplayExpectWeatherDataObserverListHasOneObserver()
        {
            var weatherData = new WeatherData();
            var currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);

            Assert.AreEqual(1, weatherData.Observers.Count);
        }
Ejemplo n.º 2
0
        private static void TestObserverPattern()
        {
            WeatherData         weatherData         = new WeatherData();
            TemperatureReporter temperatureReporter = new TemperatureReporter();

            CurrentConditionsDisplay  currentConditionsDisplay  = new CurrentConditionsDisplay(weatherData);
            StatisticsDisplay         statisticsDisplay         = new StatisticsDisplay(weatherData);
            ForecastDisplay           forecastDisplay           = new ForecastDisplay(weatherData);
            HeatIndexDisplay          heatIndexDisplay          = new HeatIndexDisplay(weatherData);
            CurrentTemperatureDisplay currentTemperatureDisplay = new CurrentTemperatureDisplay(temperatureReporter);

            Temperature t = new Temperature {
                Temp = 80
            };

            temperatureReporter.TemperatureChanged(t);

            t.Temp = 82;
            temperatureReporter.TemperatureChanged(t);
            Console.WriteLine(currentTemperatureDisplay.Display());

            t.Temp = 78;
            temperatureReporter.TemperatureChanged(t);
            Console.WriteLine(currentTemperatureDisplay.Display());

            weatherData.SetMeasurements(80, 65, 30.4);
            weatherData.SetMeasurements(82, 70, 29.2);
            weatherData.SetMeasurements(78, 90, 29.2);

            Console.WriteLine(currentConditionsDisplay.Display());
            Console.WriteLine(statisticsDisplay.Display());
            Console.WriteLine(forecastDisplay.Display());
            Console.WriteLine(heatIndexDisplay.Display());
        }
Ejemplo n.º 3
0
        private void btnAdd1_Click(object sender, RoutedEventArgs e)
        {
            WeatherData weatherData = new WeatherData();
            CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.SetMeasurements(10, 20, 30);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The Observer Pattern defines a one-to-many dependency between objects so that
        /// when one object changes state, all of its dependents are notified and updated
        /// automatically. Object which changes state is called - subject. Object which
        /// subscribes to subject is called - observer.
        ///
        /// This project uses built in observer pattern.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var wd = new WeatherData();

            var disp = new CurrentConditionsDisplay();

            using (wd.Subscribe(disp))
            {
                var wi = new WeatherInfo
                {
                    Humidity    = 80,
                    Pressure    = 65,
                    Temperature = 30.4f
                };
                wd.WeatherInfo = wi;
                Thread.Sleep(2000);
                wi = new WeatherInfo
                {
                    Humidity    = 85,
                    Pressure    = 65,
                    Temperature = 31.4f
                };
                wd.WeatherInfo = wi;
                wd.Unsubscribe(disp);
                Thread.Sleep(2000);
                wi = new WeatherInfo
                {
                    Humidity    = 60,
                    Pressure    = 65,
                    Temperature = 29.4f
                };
                wd.WeatherInfo = wi;
            }
        }
        static void Main(string[] args)
        {
            CurrentConditionsDisplay display_1, display_2;

            // Create Publisher:
            WeatherData weatherData = new WeatherData();

            // Create and register first observer:
            display_1 = new CurrentConditionsDisplay("First text display");
            display_1.Subscribe(weatherData);

            // Create and register first observer:
            display_2 = new CurrentConditionsDisplay("Second text display");
            display_2.Subscribe(weatherData);

            // Simulate availability of new measurements:
            weatherData.SetMeasurement(new Measurement(15.0f, 44.2f, 1020.0f));
            weatherData.SetMeasurement(new Measurement(16.5f, 48.9f, 1015.0f));
            weatherData.SetMeasurement(new Measurement(16.2f, 47.3f, 1025.0f));

            // Remove first observer from observable's notification list:            
            display_1.Unsubscribe();

            // Simulate availability of new measurements again:
            weatherData.SetMeasurement(new Measurement(18.0f, 50.0f, 980.0f));

            // Keap console open:
            ReadLine();

        } // Main
        public void WeatherStation()
        {
            WeatherData weatherData = new WeatherData();
            CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4f);
        }
 public static void Main(string[] args) {
     WeatherData weatherData = new WeatherData();
     CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
     
     weatherData.setMeasurements(80, 65, 30.4f);
     weatherData.setMeasurements(82, 70, 29.2f);
     weatherData.setMeasurements(78, 90, 29.2f);
 }
 public ObserverWeatherDataUnitTest()
 {
     _weatherData = new WeatherData();
     _currentConditionsDisplay = new CurrentConditionsDisplay(_weatherData);
     _forcastDisplay           = new ForcastDisplay(_weatherData);
     _statisticsDisplay        = new StatisticsDisplay(_weatherData);
     _heatIndexDisplay         = new HeatIndexDisplay(_weatherData);
 }
Ejemplo n.º 9
0
    public static void Main()
    {
        WeatherData weatherData = new WeatherData();
        CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

        weatherData.Subscribe(weatherStation);
        weatherStation.start();
    }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            WeatherData              weatherData              = new WeatherData();
            SimpleForecastDisplay    simpleForecast           = new SimpleForecastDisplay(weatherData);
            WeatherStatisticsDisplay weatherStatistics        = new WeatherStatisticsDisplay(weatherData);
            CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.StartObtainingData();
        }
Ejemplo n.º 11
0
        public void WhenCreateCurrentConditionsDisplayAndCallSetMeasurementsMethodExpectDisplayMethodReturnCurrentConditionsMessage()
        {
            var weatherData = new WeatherData();
            var currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.SetMeasurements(80, 50, 80);

            Assert.AreEqual("Current conditions: 80 F degrees and 50 % humidity", currentConditionsDisplay.Display());
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            WeatherData weatherData = new WeatherData();
            CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4f);
            weatherData.SetMeasurements(82, 70, 29.2f);
            weatherData.SetMeasurements(78, 90, 29.2f);
        }
Ejemplo n.º 13
0
        public void WhenCreateDisplayAndCallRemoveObserverExpectWeatherDataObserverListIsEmpty()
        {
            var weatherData = new WeatherData();
            var currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.RemoveObserver(currentConditionsDisplay);

            Assert.AreEqual(0, weatherData.Observers.Count);
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            WeatherData weatherData = new WeatherData();

            CurrentConditionsDisplay currentDisplay   = new CurrentConditionsDisplay(weatherData);
            HeatIndexDisplay         heatIndexDisplay = new HeatIndexDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4f);
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            WeatherData weatherData = new WeatherData();

            CurrentConditionsDisplay display = new CurrentConditionsDisplay(weatherData);

            WeatherStation weatherStation = new WeatherStation(weatherData);

            weatherStation.start();
        }
Ejemplo n.º 16
0
    public static void Test()
    {
        WeatherData weatherData = new WeatherData();

        CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

        weatherData.setMeasurements(80, 65, 30.4f);
        weatherData.setMeasurements(82, 70, 29.2f);
        weatherData.setMeasurements(78, 90, 29.2f);
    }
        public void ShouldObserveUpdate()
        {
            var weatherData = new WeatherData();

            var currentDisplay = new CurrentConditionsDisplay(weatherData);

            var forecastDisplay = new ForecastDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4f);
        }
Ejemplo n.º 18
0
    static void Main()
    {
        WeatherData weatherData = new WeatherData();
        CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
        AnotherDisplay           heatDisplay    = new AnotherDisplay(weatherData);

        weatherData.SetMeasurments(80, 65, 30.4f);
        weatherData.SetMeasurments(82, 70, 29.8f);
        weatherData.SetMeasurments(78, 90, 29.7f);
    }
        public void WeatherData_CanRemoveObservers()
        {
            WeatherData      weatherData       = new WeatherData();
            IWeatherObserver statisticsDisplay = new StatisticsDisplay(weatherData);
            IWeatherObserver forecastDisplay   = new ForecastDisplay(weatherData);
            IWeatherObserver conditionsDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.RemoveObserver(statisticsDisplay);
            Assert.AreEqual(2, weatherData.SubscriberCount);
        }
        public void WeatherData_ShouldBeEmpty_WhenUnregisterObservers()
        {
            //Arrange
            var weather = new WeatherData();
            var display = new CurrentConditionsDisplay(weather);

            weather.UnregisterObserver(display);

            //Assert
            weather.Observers.Should().BeEmpty();
        }
Ejemplo n.º 21
0
        static void RunObserver()
        {
            WeatherData weatherData = new WeatherData();
            CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4f);
            weatherData.SetMeasurements(82, 70, 29.2f);
            weatherData.SetMeasurements(78, 90, 29.2f);

            Console.ReadKey();
        }
Ejemplo n.º 22
0
        public static void Run()
        {
            var weatherData = new WeatherData();
            var currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
            var forecastDisplay          = new ForecastDisplay(weatherData);
            var statisticsDisplay        = new StatisticsDisplay(weatherData);

            weatherData.SetMeasurements(30, 80, 50);
            weatherData.SetMeasurements(15, 80, 0);
            weatherData.SetMeasurements(25, 50, 0);
        }
        public static void Start()
        {
            WeatherData weatherData = new WeatherData();
            CurrentConditionsDisplay currentDisplay    = new CurrentConditionsDisplay(weatherData);
            StatisticsDisplay        statisticsDisplay = new StatisticsDisplay(weatherData);
            ForecastDisplay          forecastDisplay   = new ForecastDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4f);
            weatherData.SetMeasurements(82, 70, 29.2f);
            weatherData.SetMeasurements(78, 90, 29.2f);
        }
Ejemplo n.º 24
0
        static void Main(string[] args)
        {
            var weatherData = new WeatherData();

            var currentDisplay = new CurrentConditionsDisplay(weatherData);
            var heatDisplay    = new HeatIndexDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4F);
            weatherData.SetMeasurements(82, 70, 29.2F);
            weatherData.SetMeasurements(78, 90, 29.2F);
        }
Ejemplo n.º 25
0
        private static void Main()
        {
            var weatherData = new WeatherData();
            var currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);

            weatherData.SetMeasurement(10, 90, 80);
            weatherData.SetMeasurement(12, 85, 75);

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
Ejemplo n.º 26
0
        public void UpdateObservers()
        {
            WeatherData wd = new WeatherData();

            IObserver fd  = new ForecastDisplay(wd);
            IObserver sd  = new StatisticsDisplay(wd);
            IObserver ccd = new CurrentConditionsDisplay(wd);
            IObserver hi  = new HeatIndexDisplay(wd);

            wd.SetMeasurements(5.5f, 200f, 0.05f);
        }
Ejemplo n.º 27
0
        public void WhenCreateDisplayAndCallSetMeasurementsMethodExpectAllObserversAreNotified()
        {
            var weatherData = new WeatherData();
            var currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
            var forecastDisplay          = new ForecastDisplay(weatherData);

            weatherData.SetMeasurements(80, 50, 80);

            Assert.AreEqual("Current conditions: 80 F degrees and 50 % humidity", currentConditionsDisplay.Display());
            Assert.AreEqual("Forecast: Improving weather on the way!", forecastDisplay.Display());
        }
Ejemplo n.º 28
0
        static void Main(string[] args)
        {
            WeatherData weatherData = new WeatherData();

            CurrentConditionsDisplay currentDisplay  = new CurrentConditionsDisplay(weatherData);
            StatisticsDisplay        statsDisplay    = new StatisticsDisplay(weatherData);
            ForecastDisplay          forecastDisplay = new ForecastDisplay(weatherData);

            weatherData.setMeasurements(80, 65, 30.4f);
            weatherData.setMeasurements(82, 70, 29.2f);
            weatherData.setMeasurements(78, 90, 29.2f);
        }
Ejemplo n.º 29
0
        static void Main(string[] args)
        {
            var station = new WeatherData();
            var display = new CurrentConditionsDisplay(station);

            station.SetMeasurements("24", "70", "1024");
            station.SetMeasurements("25", "60", "1024");
            station.SetMeasurements("26", "50", "1024");
            station.SetMeasurements("27", "40", "1024");

            Console.ReadKey();
        }
Ejemplo n.º 30
0
        public void Main(string[] args)
        {
            WeatherData weatherData = new WeatherData();
            CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);
            StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
            ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
            HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData);

            weatherData.SetMeasurements(80, 65, 30.4f);
            weatherData.SetMeasurements(82, 70, 29.2f);
            weatherData.SetMeasurements(78, 90, 29.2f);
        }
Ejemplo n.º 31
0
        static void Main(string[] args)
        {
            var data     = new WeatherData();
            var display1 = new CurrentConditionsDisplay(data);
            var display2 = new ForecastDisplay(data);
            var display3 = new StatisticsDisplay(data);
            var display4 = new HeatIndexDisplay(data);

            data.SetMeasurements(21.3f, 23.1f, 52.4f);
            data.SetMeasurements(42.3f, 33.1f, 52.4f);
            data.SetMeasurements(19.3f, 63.1f, 52.4f);
            data.SetMeasurements(80.0f, 0.4f, 0f);
        }
Ejemplo n.º 32
0
        private static void ObserverPattern()
        {
            WeatherData weatherData = new WeatherData();

            CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
            StatisticsDisplay        statisticsDisplay        = new StatisticsDisplay(weatherData);
            ForecastDisplay          forecastDisplay          = new ForecastDisplay(weatherData);
            HeatIndexDisplay         heatIndexDisplay         = new HeatIndexDisplay(weatherData);

            weatherData.setMeasurements(80, 65, 30.4f);
            weatherData.setMeasurements(82, 70, 29.2f);
            weatherData.setMeasurements(78, 90, 29.2f);
        }
        public static void Main(string[] args) {
            WeatherObservable weatherReporter = new WeatherObservable();
            WeatherData wd = new WeatherData();
            wd.SetMeasurements(48.0f, .50f, 10.0f);

            CurrentConditionsDisplay display = new CurrentConditionsDisplay();
            using (IDisposable disposable = weatherReporter.Subscribe(display)) {
                weatherReporter.UpdateWeather(wd, false);
                wd = new WeatherData();
                wd.SetMeasurements(105.0f, .89f, 32.0f);
                weatherReporter.UpdateWeather(wd, false);
                wd = new WeatherData();
                wd.SetMeasurements(17.5f, .16f, 12.0f);
                weatherReporter.UpdateWeather(wd, false);
                weatherReporter.CloseAllStationReports();
            }
            
        }
Ejemplo n.º 34
0
        public static void Main(string[] args)
        {
            WeatherMonitor monitor = new WeatherMonitor();
            ForecastDisplay forecast = new ForecastDisplay();
            CurrentConditionsDisplay currentConditions = new CurrentConditionsDisplay();
            StatisticsDisplay statistics = new StatisticsDisplay();
            HeatIndexDisplay heatIndex = new HeatIndexDisplay();

            monitor.SetMeasurments(new WeatherData(80, 65, 30.4f));
            forecast.Subscribe(monitor);
            currentConditions.Subscribe(monitor);
            statistics.Subscribe(monitor);
            heatIndex.Subscribe(monitor);
            monitor.SetMeasurments(new WeatherData(82, 70, 29.2f));
            statistics.Unsubscribe();
            monitor.SetMeasurments(new WeatherData(78, 90, 29.2f));
            monitor.SetMeasurments(null);
            //weatherData.SetMeasurments(82, 70, 29.2f);
            //weatherData.SetMeasurments(78, 90, 29.2f);
            Console.ReadLine();
        }