Advertisement
Fhernd

WeatherData.cs

Mar 12th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7.  
  8. namespace HeadFirstDesignPatterns.Ch02TheObserverPattern.WeatherStation
  9. {
  10.     class WeatherData : Subject
  11.     {
  12.         private ArrayList observers;
  13.         private float temperature;
  14.         private float humidity;
  15.         private float pressure;
  16.  
  17.         public WeatherData()
  18.         {
  19.             observers = new ArrayList();
  20.         }
  21.  
  22.         public void RegisterObserver(Observer o)
  23.         {
  24.             observers.Add(o);
  25.         }
  26.  
  27.         public void RemoveObserver(Observer o)
  28.         {
  29.             int i = observers.IndexOf(o);
  30.             if (i > 0)
  31.             {
  32.                 observers.Remove(i);
  33.             }
  34.         }
  35.  
  36.         public void NotifyObservers()
  37.         {
  38.             for (int i = 0; i < observers.Count; i++)
  39.             {
  40.                 Observer observer = (Observer)observers[i];
  41.                 observer.update(temperature, humidity, pressure);
  42.             }
  43.         }
  44.  
  45.         public void measurementsChanged()
  46.         {
  47.             NotifyObservers();
  48.         }
  49.  
  50.         public void setMeasurements(float temperature, float humidity, float pressure)
  51.         {
  52.             this.temperature = temperature;
  53.             this.humidity = humidity;
  54.             this.pressure = pressure;
  55.             measurementsChanged();
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement