Ejemplo n.º 1
0
        private static void Observer()
        {
            var weatherStation = new WeatherStation();

            weatherStation.Add(new TV());
            weatherStation.Add(new CellPhone());
            weatherStation.SetCurrentWeather(new WeatherPayload(10.5m));
            weatherStation.SetCurrentWeather(new WeatherPayload(10.4m));
            weatherStation.SetCurrentWeather(new WeatherPayload(10.6m));
        }
Ejemplo n.º 2
0
        public void Notify_Notifies()
        {
            WeatherStation s         = new WeatherStation();
            var            observer  = new TestObserver();
            var            observer2 = new TestObserver();

            s.Add(observer);
            s.Add(observer2);
            s.ChangeWeather();

            s.Notify();

            Assert.True(observer.wasNotified);
            Assert.True(observer2.wasNotified);
        }
Ejemplo n.º 3
0
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello World");

        WeatherStation wStation = new WeatherStation();
        SmartPhone     phone    = new SmartPhone(wStation);
        WebSite        site     = new WebSite(wStation);

        wStation.Add(phone);
        wStation.SetTemperature(12.3);
        wStation.Add(site);
        wStation.SetTemperature(13.2);
        wStation.Remove(phone);
        wStation.SetTemperature(11.9);
    }
Ejemplo n.º 4
0
        public void Add_AddsObserver()
        {
            WeatherStation s = new WeatherStation();

            s.Add(new TestObserver());

            Assert.Single(s.observers);
        }
Ejemplo n.º 5
0
        private static void Observer_Pattern()
        {
            // TODO The Observer pattern defines one to many dependencies between objects so that when one object changes its state
            // TODO all of its dependencies are notified and updated automatically

            var       weatherStation = new WeatherStation();
            IObserver phoneDisplay   = new PhoneDisplay(weatherStation);
            IObserver tabletDisplay  = new TabletDisplay(weatherStation);

            // push the observer in the list
            weatherStation.Add(phoneDisplay);
            weatherStation.Add(tabletDisplay);

            Console.WriteLine("Observable Weather Station received new Temperature so it will update the observers");
            //poll the date from observable for each observer in the list
            weatherStation.Notify();
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            WeatherStation station = new WeatherStation();

            PrintMediaDisplay  printMediaDisplay  = new PrintMediaDisplay(station);
            NewsChannelDisplay newsChannelDisplay = new NewsChannelDisplay(station);

            station.Add(printMediaDisplay);
            station.Add(newsChannelDisplay);

            var timer = new System.Threading.Timer((e) =>
            {
                station.Notify();
            }, null, TimeSpan.Zero, TimeSpan.FromSeconds(10));

            Console.ReadKey();
        }
Ejemplo n.º 7
0
        public void Remove_RemovesObserver()
        {
            WeatherStation s = new WeatherStation();

            var observer = new TestObserver();

            s.Add(observer);
            s.Remove(observer);

            Assert.Empty(s.observers);
        }