Example #1
0
        static void Main()
        {
            CWeatherData weatherDataIn  = new CWeatherData();
            CWeatherData weatherDataOut = new CWeatherData();

            Display display = new Display(weatherDataIn, weatherDataOut);

            weatherDataIn.RegisterObserver(display, 2);
            weatherDataOut.RegisterObserver(display, 2);

            StatsDisplay statsDisplay = new StatsDisplay(weatherDataIn, weatherDataOut);

            weatherDataIn.RegisterObserver(statsDisplay, 0);
            weatherDataOut.RegisterObserver(statsDisplay, 1);

            StatsDisplay statsDisplay1 = new StatsDisplay(weatherDataIn, weatherDataOut);

            weatherDataOut.RegisterObserver(statsDisplay1, 0);

            weatherDataIn.SetMeasurements(3, 0.7, 760);
            weatherDataOut.SetMeasurements(4, 0.8, 761);

            weatherDataOut.RemoveObserver(statsDisplay);

            weatherDataIn.SetMeasurements(10, 0.8, 761);
            weatherDataOut.SetMeasurements(-10, 0.8, 761);
        }
Example #2
0
        public CWeatherStationPro()
        {
            CWeatherData wd      = new CWeatherData();
            CDisplay     display = new CDisplay();

            wd.RegisterObserver(display);

            CStatsDisplay statsDisplay = new CStatsDisplay();

            wd.RegisterObserver(statsDisplay);

            wd.SetMeasurements(3, 0.7, 760, 10, 0);
            wd.SetMeasurements(4, 0.8, 761, 8, 630);
        }
        public void When_identical_observers_are_set_next_one_isnt_added()
        {
            CTestingDisplay.ResetUpdateCounter();

            CWeatherData    wd       = new CWeatherData();
            CTestingDisplay display1 = new CTestingDisplay();

            wd.RegisterObserver(display1, 2);
            wd.RegisterObserver(display1, 1);

            display1.SetTestingValues(3, 0.7, 760);
            wd.SetMeasurements(3, 0.7, 760);
            display1.CheckUpdateCounter(0);
        }
Example #4
0
        public CWeatherStationProEvents()
        {
            CWeatherData wd      = new CWeatherData();
            CDisplay     display = new CDisplay();

            wd.RegisterObserver(display);
            wd.OnTemperatureChanged += display.UpdateTemperature;
            wd.Temperature           = 200;
            CStatsDisplay statsDisplay = new CStatsDisplay();

            wd.RegisterObserver(statsDisplay);

            wd.SetMeasurements(3, 0.7, 760, 10, 0);
            wd.SetMeasurements(4, 0.8, 761, 8, 630);
        }
Example #5
0
        public void RemoveObserverInUpdate_ShouldNotThrownException()
        {
            CWeatherData       wd = new CWeatherData();
            TestUpdateObserver to = new TestUpdateObserver(wd);

            wd.RegisterObserver(to, 1);
            wd.SetMeasurements(3, 0.7, 760);
        }
        public void TestDeliteObserver()
        {
            var weatherData    = new CWeatherData();
            var customObserver = new CustomObserver(weatherData);

            weatherData.RegisterObserver(customObserver, 0);
            weatherData.SetMeasurements(0, 0, 0);
        }
Example #7
0
        public CWeatherStation()
        {
            CWeatherData wd      = new CWeatherData();
            CDisplay     display = new CDisplay();

            wd.RegisterObserver(display);

            CStatsDisplay statsDisplay = new CStatsDisplay();

            wd.RegisterObserver(statsDisplay);

            wd.SetMeasurements(3, 0.7, 760);
            wd.SetMeasurements(4, 0.8, 761);

            wd.RemoveObserver(statsDisplay);

            wd.SetMeasurements(10, 0.8, 761);
            wd.SetMeasurements(-10, 0.8, 761);
        }
Example #8
0
        static void Main()
        {
            CWeatherData wd = new CWeatherData();

            Display display = new Display();

            wd.RegisterObserver(display, 1);

            StatsDisplay statsDisplay = new StatsDisplay();

            wd.RegisterObserver(statsDisplay, 0);

            wd.SetMeasurements(3, 0.7, 760, 4, 60);
            wd.SetMeasurements(4, 0.8, 761, 2, 90);

            wd.RemoveObserver(display);

            wd.SetMeasurements(10, 0.7, 760, 4, 270);
            wd.SetMeasurements(-10, 0.8, 761, 8, 180);
        }
Example #9
0
        static void Main(string [] args)
        {
            var wd           = new CWeatherData();
            var display1     = new CDisplay();
            var display2     = new CDisplay();
            var statsDisplay = new CStatsDisplay();

            wd.RegisterObserver(display1, 10);
            wd.RegisterObserver(display2, 3);
            wd.RegisterObserver(statsDisplay, 4);

            wd.SetMeasurements(3, 0.7, 760);
            wd.SetMeasurements(4, 0.8, 761);

            wd.RemoveObserver(display1);
            wd.RemoveObserver(display2);

            wd.SetMeasurements(10, 0.8, 761);
            wd.SetMeasurements(-10, 0.8, 761);
        }
Example #10
0
        public void Observers_WithHigherPriority_ShouldBeNotifiedFirst()
        {
            var wd           = new CWeatherData();
            var testDisplay1 = new TestPriorityObserver(1);
            var testDisplay2 = new TestPriorityObserver(2);
            var testDisplay3 = new TestPriorityObserver(3);

            wd.RegisterObserver(testDisplay1, 1);
            wd.RegisterObserver(testDisplay2, 2);
            wd.RegisterObserver(testDisplay3, 3);

            var sw = new StringWriter();

            Console.SetOut(sw);
            Console.SetError(sw);
            wd.SetMeasurements(3, 0.7, 760);
            string result = sw.ToString();

            Assert.AreEqual("3;2;1;", result);
        }
Example #11
0
        public void Can_remove_observer_on_update()
        {
            CWeatherData           wd       = new CWeatherData();
            CTestRemovebleObserver display1 = new CTestRemovebleObserver(wd);

            wd.RegisterObserver(display1);
            wd.SetMeasurements(3, 0.7, 760);

            Assert.IsTrue(display1.IsRemoved);
            display1.IsRemoved = false;
            wd.SetMeasurements(4, 0.8, 761);
            Assert.IsFalse(display1.IsRemoved);
        }
Example #12
0
        public void TestOneObserver()
        {
            CWeatherData weatherData = new CWeatherData();
            CDisplay     cDisplay    = new CDisplay();
            ObserverWithPriority <SWeatherInfo>         observer     = new ObserverWithPriority <SWeatherInfo>(cDisplay, 2);
            List <ObserverWithPriority <SWeatherInfo> > observerList = new List <ObserverWithPriority <SWeatherInfo> >();

            observerList.Add(observer);


            weatherData.RegisterObserver(cDisplay, 2);
            Assert.IsTrue(CompareObserverList(observerList, weatherData.GetObservers()));
        }
        public void Can_add_observer()
        {
            CTestingDisplay.ResetUpdateCounter();

            CWeatherData    wd       = new CWeatherData();
            CTestingDisplay display1 = new CTestingDisplay();

            wd.RegisterObserver(display1);

            display1.SetTestingValues(3, 0.7, 760);
            wd.SetMeasurements(3, 0.7, 760);
            display1.CheckUpdateCounter(0);
        }
Example #14
0
        public void TestSomeObserver()
        {
            List <ObserverWithPriority <SWeatherInfo> > observerList = new List <ObserverWithPriority <SWeatherInfo> >();
            var weatherData   = new CWeatherData();
            var cDisplay      = new CDisplay();
            var cDisplay1     = new CDisplay();
            var statsDisplay  = new CStatsDisplay();
            var statsDisplay1 = new CStatsDisplay();
            var cDisplay2     = new CDisplay();

            observerList.Add(new ObserverWithPriority <SWeatherInfo>(cDisplay1, 3));
            observerList.Add(new ObserverWithPriority <SWeatherInfo>(statsDisplay, 2));
            observerList.Add(new ObserverWithPriority <SWeatherInfo>(statsDisplay1, 2));
            observerList.Add(new ObserverWithPriority <SWeatherInfo>(cDisplay, 1));
            observerList.Add(new ObserverWithPriority <SWeatherInfo>(cDisplay2, 0));

            weatherData.RegisterObserver(cDisplay, 1);
            weatherData.RegisterObserver(statsDisplay, 2);
            weatherData.RegisterObserver(cDisplay1, 3);
            weatherData.RegisterObserver(statsDisplay1, 2);
            weatherData.RegisterObserver(cDisplay2, 0);
            Assert.IsTrue(CompareObserverList(observerList, weatherData.GetObservers()));
        }
        public void When_priority_is_set_observers_updates_in_order_of_priority()
        {
            CTestingDisplay.ResetUpdateCounter();

            CWeatherData wd = new CWeatherData();

            CTestingDisplay display1 = new CTestingDisplay();
            CTestingDisplay display2 = new CTestingDisplay();
            CTestingDisplay display3 = new CTestingDisplay();

            wd.RegisterObserver(display2, 2);
            wd.RegisterObserver(display1, 1);
            wd.RegisterObserver(display3, 3);

            display1.SetTestingValues(3, 0.7, 760);
            display2.SetTestingValues(3, 0.7, 760);
            display3.SetTestingValues(3, 0.7, 760);

            wd.SetMeasurements(3, 0.7, 760);

            display1.CheckUpdateCounter(0);
            display2.CheckUpdateCounter(1);
            display3.CheckUpdateCounter(2);
        }
Example #16
0
        public void NotifyObservers_WithWeatherStationType()
        {
            var weatherDataIn  = new CWeatherData();
            var weatherDataOut = new CWeatherData();

            var display = new DisplayDuo(weatherDataIn, weatherDataOut);

            weatherDataIn.RegisterObserver(display, 1);
            weatherDataOut.RegisterObserver(display, 1);

            var sw = new StringWriter();

            Console.SetOut(sw);
            Console.SetError(sw);

            weatherDataIn.SetMeasurements(3, 0.7, 760);
            weatherDataOut.SetMeasurements(4, 0.8, 761);

            string result   = sw.ToString();
            string expected = "Inside;Outside;";

            Assert.AreEqual(expected, result);
        }