Exemplo n.º 1
0
 static void Main(string[] args)
 {
     WeatherData weatherData = new WeatherData();
     CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(weatherData);
     ForecastDisplay fd = new ForecastDisplay(weatherData);
     StatisticsDisplay sd = new StatisticsDisplay(weatherData);
     ThirdPartyDisplay tpd = new ThirdPartyDisplay(weatherData);
     weatherData.SetMeasurements(15, 75, 30);
     weatherData.SetMeasurements(18, 80, 31.2);
     weatherData.SetMeasurements(18, 75, 32.8);
     Console.ReadKey(true);
 }
Exemplo n.º 2
0
        static void Main()
        {
            WeatherData wd = new WeatherData();
            CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(wd);
            StatisticsDisplay sd = new StatisticsDisplay(wd);
            HeatIndexDisplay hid = new HeatIndexDisplay(wd);

            wd.SetMeasurements(78, 90, 29.2f);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
Exemplo n.º 3
0
        static void Main()
        {
            // First example
            ConcreteSubject s = new ConcreteSubject();
            s.Attach(new ConcreteObserver(s, "x"));
            s.Attach(new ConcreteObserver(s, "y"));
            s.Attach(new ConcreteObserver(s, "z"));
            s.SubjectState = "abc";
            s.Notify();

            IBM ibm = new IBM("IBM", 120.00);
            ibm.Attach(new Investor("Sorros"));
            ibm.Attach(new Investor("Berkshire"));

            ibm.Price = 120.10;
            ibm.Price = 121.00;
            ibm.Price = 120.50;
            ibm.Price = 120.75;
            ibm.Notify();

            // Second example
            BaggageHandler provider = new BaggageHandler();
            ArrivalsMonitor observer1 = new ArrivalsMonitor("BaggageClaimMonitor1");
            ArrivalsMonitor observer2 = new ArrivalsMonitor("SecurityExit");

            provider.BaggageStatus(712, "Detroit", 3);
            observer1.Subscribe(provider);
            provider.BaggageStatus(712, "Kalamazoo", 3);
            provider.BaggageStatus(400, "New York-Kennedy", 1);
            provider.BaggageStatus(712, "Detroit", 3);
            observer2.Subscribe(provider);
            provider.BaggageStatus(511, "San Francisco", 2);
            provider.BaggageStatus(712);
            observer2.Unsubscribe();
            provider.BaggageStatus(400);
            provider.LastBaggageClaimed();

            // Third example
            var weatherData = new WeatherData();
            var forecastDisplay = new ForecastDisplay(weatherData);
            var heatIndexDisplay = new HeatIndexDisplay(weatherData);
            weatherData.setMeasurements(80, 65, 30.4f);
        }
Exemplo n.º 4
0
 static void Main(string[] args)
 {
     var weather = new WeatherData();
     var observer = new CurrentConditionsDisplay(weather);
     //其他观察者
     //
     weather.setMeasurements(3, 4, 6);
     Console.ReadKey();
 }
Exemplo n.º 5
0
 public ComputerDevice(WeatherData weatherdata)
 {
     this.weatherdata = weatherdata;
     this.weatherdata.AddObserver(this);
 }
Exemplo n.º 6
0
 public StatisticsDisplay(WeatherData weatherData) //pull observer
 {
     this.weatherData = weatherData;
 }
 public ForecastDisplay(WeatherData weatherData)
 {
     this.weatherData = weatherData;
     currentPressure  = 29.2f;
     weatherData.registerObserver(this);
 }
Exemplo n.º 8
0
 public HeatIndexDisplay(WeatherData weatherData)
 {
     this.weatherData = weatherData;
     heatIndex        = 0.0f;
     weatherData.registerObserver(this);
 }
Exemplo n.º 9
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();
        }
Exemplo n.º 10
0
 public Forecast(WeatherData newWeatherData)
 {
     this.weatherData              = newWeatherData;
     this.weatherData.ChangeEvent += new WeatherData.ChangeHandler(Update);
 }
Exemplo n.º 11
0
 public Statistics(WeatherData newWeatherData)
 {
     this.weatherData              = newWeatherData;
     this.weatherData.ChangeEvent += new WeatherData.ChangeHandler(Update);
 }
Exemplo n.º 12
0
 public Piano(WeatherData newWeatherData)
 {
     this.weatherData              = newWeatherData;
     this.weatherData.ChangeEvent += new WeatherData.ChangeHandler(Update);
 }
Exemplo n.º 13
0
 public CurrentConditions(WeatherData newWeatherData)
 {
     this.weatherData              = newWeatherData;
     this.weatherData.ChangeEvent += new WeatherData.ChangeHandler(Update);
 }
Exemplo n.º 14
0
 public MobileDevice(WeatherData weatherdata)
 {
     this.weatherdata = weatherdata;
     this.weatherdata.AddObserver(this);
 }
Exemplo n.º 15
0
 public SomeAnotherDevice(WeatherData weatherdata)
 {
     this.weatherdata = weatherdata;
     this.weatherdata.AddObserver(this);
 }
 public StatisticsDisplay(WeatherData weatherData)
 {
     this._weatherData = weatherData;
     weatherData.RegisterObserver(this);
 }
 public ForecastDisplay(WeatherData weatherData)
 {
     this.weatherData = weatherData;
     weatherData.registerObserver(this);
 }
Exemplo n.º 18
0
 public void setWeatherData(WeatherData weatherData)
 {
     this.weatherData = weatherData;
 }