コード例 #1
0
        public void Implement()
        {
            Console.WriteLine("\nLearning Observer Pattern\n");
            WeatherData weatherData = new WeatherData(temperature: 40, pressure: 120, humidity: 20);

            Console.WriteLine(weatherData.ToString());
            IObserver sObserver = new StatisticsDisplay();

            weatherData.RegisterObserver(sObserver);
            IObserver fObserver = new ForecastDisplay();

            weatherData.RegisterObserver(fObserver);
            IObserver gObserver = new GeneralDisplay();

            weatherData.RegisterObserver(gObserver);
            weatherData.NotifyObserver();
            sObserver.Display();
            fObserver.Display();
            gObserver.Display();

            weatherData.DataChanged(temperature: 20, pressure: 80, humidity: 10);  //Just a dummy method which updates the weather data
            Console.WriteLine($"\nWeather changed {weatherData.ToString()}");
            sObserver.Display();
            fObserver.Display();
            gObserver.Display();

            Console.WriteLine("\nObserver Removed: General Display");
            weatherData.RemoveObserver(gObserver);
            weatherData.DataChanged(temperature: -20, pressure: 20, humidity: 0);  //Just a dummy method which updates the weather data
            sObserver.Display();
            fObserver.Display();
            gObserver.Display();
        }
コード例 #2
0
        public void weather_data_updates_statistics_correctly()
        {
            var wd      = new WeatherData();
            var display = new StatisticsDisplay(wd);

            wd.SetMeasurements(20, 15, 73);
            Assert.AreEqual("Avg/Max/Min Temperature: 20/20/20", display.Display());

            wd.SetMeasurements(40, 15, 73);
            Assert.AreEqual("Avg/Max/Min Temperature: 30/40/20", display.Display());

            wd.SetMeasurements(0, 15, 73);
            Assert.AreEqual("Avg/Max/Min Temperature: 20/40/0", display.Display());
        }
コード例 #3
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());
        }
コード例 #4
0
        public void WhenCreateStatisticsDisplayAndCallSetMeasurementsMethodOnceExpectDisplayMethodReturnStatisticsMessageAvg80Max80Min80()
        {
            var weatherData       = new WeatherData();
            var statisticsDisplay = new StatisticsDisplay(weatherData);

            weatherData.SetMeasurements(80, 50, 80);

            Assert.AreEqual("Avg/Max/Min temperature = 80 / 80 / 80", statisticsDisplay.Display());
        }
コード例 #5
0
        public void weather_data_updates_observers_correctly()
        {
            var wd         = new WeatherData();
            var conditions = new CurrentConditionsDisplay(wd);
            var heat       = new HeatIndexDisplay(wd);
            var stats      = new StatisticsDisplay(wd);
            var forecast   = new ForecastDisplay(wd);

            wd.SetMeasurements(80, 65, 30.4f);
            Assert.AreEqual("Current Conditions: 80 celsius and humidity 65", conditions.Display());
            Assert.AreEqual("Heat index is 82.95535", heat.Display());
            Assert.AreEqual("Avg/Max/Min Temperature: 80/80/80", stats.Display());
            Assert.AreEqual("Improving weather on the way!", forecast.Display());

            wd.SetMeasurements(78, 90, 29.2f);
            Assert.AreEqual("Current Conditions: 78 celsius and humidity 90", conditions.Display());
            Assert.AreEqual("Heat index is 83.64967", heat.Display());
            Assert.AreEqual("Avg/Max/Min Temperature: 79/80/78", stats.Display());
            Assert.AreEqual("Weather is getting bad!", forecast.Display());
        }
コード例 #6
0
        public void StatisticsDisplay_ShouldDisplay_WhenMeasurementsChanged()
        {
            //Act
            _weatherData.SetMeasurements(80, 63, 31.2f);
            _weatherData.SetMeasurements(81, 63, 29.92f);
            _weatherData.SetMeasurements(84, 63, 29.92f);
            var display = _statisticsDisplay.Display();

            //Assert
            display.Should().Be("Avg/Max/Min temperature = 81.67F/84F/80F");
            _statisticsDisplay.NumberOfReadings.Should().Be(3);
        }
コード例 #7
0
        public void Test_WeatherStationHeatIndex()
        {
            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);

            var result = statisticsDisplay.Display();
        }
コード例 #8
0
        public void WeatherStationTest()
        {
            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);

            currentDisplay.Display();
            statisticsDisplay.Display();
            forecastDisplay.Display();
        }