Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Starting Observer demo:");
            WeatherData weatherData           = new WeatherData();
            CurrentConditionsDisplay display1 = new CurrentConditionsDisplay(weatherData);

            weatherData.RegisterObserver(display1);
            weatherData.SetMeasurements(20.3f, 50, 12);
            weatherData.SetMeasurements(23.1f, 55, 17);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            WeatherData weatherData = new WeatherData();

            CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData);

            weatherData.SetMeasurement(80, 65, 30.4f);
            weatherData.RegisterObserver(currentConditionDisplay);
            weatherData.SetMeasurement(80, 65, 30.4f);
            weatherData.Remove(currentConditionDisplay);
            weatherData.SetMeasurement(80, 65, 30.4f);
        }
Exemplo n.º 3
0
        static void Main()
        {
            ISubject  weatherData       = new WeatherData();
            IObserver conditionsDisplay = new ConditionsDisplay();

            weatherData.RegisterObserver(conditionsDisplay);

            weatherData.NotifyObservers(45, 10);
            weatherData.NotifyObservers(35, 25);

            Console.ReadLine();
        }
 public StatisticsDisplay(WeatherData weatherData)
 {
     this._weatherData = weatherData;
     weatherData.RegisterObserver(this);
 }
Exemplo n.º 5
0
 public ForecastDisplay(WeatherData weatherData)
 {
     this._weatherData = weatherData;
     weatherData.RegisterObserver(this);
 }
 private void Start()
 {
     _weatherData.RegisterObserver(_currentConditionsDisplay);
     _weatherData.RegisterObserver(_statisticsDisplay);
     StartCoroutine(_MeasurementsChangeCoroutine());
 }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            /*
             * The Observer Pattern
             *
             * Example:
             * A publisher creates a new magazine and begins publishing issues.
             * You subscribve and receive issues as along as you stay subscribed.
             * You can unsubscribe at any time.
             * Others can also subscribe.
             * Other can also unsubscribe.
             * If Publisher ceases business, you stop receiving issues.
             *
             * Allows:
             * -Subjests and Observers are "loosely coupled"
             * -Strive for loosely coupled designs between objects that interact. (Design Principle #4)
             *
             * Implementation (Push):
             *
             * We define 3 interfaces:
             *  -ISubject (implemented by publisher, stores the list of subscribers, notifies on updates)
             *  -IObserver (implemented by subscriber, update method)
             *  -IDisplayElement (implemented by subscriber, display method)
             *
             * We define main class WatherData:
             *  -WatherData that implements ISubject
             *  -Notifies observers(subscribers) though method Update()
             *  -This doesn't know anything about observers
             *  -Subject doesn't care it keeps doing its job
             *
             * We define subscriber classes:
             * -Each implements interfaces IObserver and IDisplayElement just to display info
             * -Each receives Subject(Publisher) in constructor, subscribes itself to it, and keeps it in private field
             * -To become an observer it needs to implement IObserver
             *
             */

            //create concrete subject(publisher)
            WeatherData weatherData = new WeatherData();

            //create Observer(subscriber) and register with Subject(publisher)
            CurrentConditionsDisplay currentConditions = new CurrentConditionsDisplay(weatherData); //subscribe in constructor of observer(subscriber), PUSH subscription

            //create more Observers(subscribers)
            weatherData.RegisterObserver(new StatisticsDisplay(weatherData)); //subscribe in method of subject(publisher), PULL subscription

            ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);

            //refresh data at Subject(publisher), simulate new data from sensors
            weatherData.SetMeasurements(new Data()
            {
                Temp     = 80,
                Humidity = 65,
                Pressure = 29.2
            });

            weatherData.SetMeasurements(new Data()
            {
                Temp     = 82,
                Humidity = 70,
                Pressure = 30.4
            });

            weatherData.SetMeasurements(new Data()
            {
                Temp     = 78,
                Humidity = 90,
                Pressure = 29.4
            });

            Console.ReadLine();
        }