Design Patterns: Observer pattern (a)

  • Observer Pattern defines a theme between objects should rely more and more on the observer, so that when one object changes state, all its dependents are notified and updated automatically.
  • Role :
    • Theme (Subject) : the theme is a interface that defines the methods required to achieve specific topics
    • The observer (the Observer) : a viewer interface, which specifies a method for updating data specific viewer.
    • Specific topics (ConcreteSubject) : a particular instance of the theme implemented theme interface class comprises the instance data may change frequently. Use a set of specific topics, such as ArrayList, to store reference observer, in order to inform specific observer when data changes.
    • DETAILED observer (the Observer Concrete) : implement the specific instance of a viewer is the viewer interface. Specific observer contains references can be stored on specific themes theme interface variables, in order to make specific observer specific topics add yourself to the collection of a specific topic, to become its observer, or make this specific topic from their specific observer list topic is deleted, so he was no longer its viewers.
  • Many relationship :
    • Using the observer pattern, the theme object has a state, and these states can be controlled. In other words, there is "a" with a theme state. On the other hand, the use of these observer status, although these states do not belong to them. There are many observers rely theme to tell them when to change the state. This creates a relationship: "a" theme corresponds to the "multiple" relationship of the observer.
  • Demo background 
    • Design of a meteorological observation station, measuring temperature, humidity, atmospheric pressure, etc., have various bulletin boards such as signboards temperature, comfort bulletin boards, bulletin boards, etc. weather forecast. Whenever changes in weather data, the data needs to these billboards is updated automatically.
  • Code
    • Interface theme: ISubject
      public interface ISubject
      {
          void RegisterObserver(IObserver o);
          void RemoveObserver(IObserver o);
          void NotifyObserver();
      }
    • Observer Interface: IObserver
      public  interface IObserver
      {
          void Update(float temp, float humidity, float pressure);
      }
    • Specific topics: WeatherData
      public class WeatherData:ISubject
      {
          private ArrayList observers;
          private float temperature;
          private float humidity;
          private float pressure;
          public WeatherData()
              {
                  observers = new ArrayList();
              }
          public  void RegisterObserver (the IObserver)
          {
              observers.Add(o);
          }
      
          public void RemoveObserver(IObserver o)
          {
              int i = observers.IndexOf(o);
              if (i >= 0)
              {
                  observers.RemoveAt(i);
              }
          }
      
          public void NotifyObserver()
          {
              for (int i = 0; i < observers.Count; i++)
              {
                  IObserver observer = (IObserver)observers[i];
                  observer.Update(temperature, humidity, pressure);
              }
          }
          public void messurementsChanged()
              {
                  NotifyObserver();
              }
          public void SetMessureMents(float temperature, float humidity, float pressure)
              {
                  this.temperature = temperature;
                  this.humidity = humidity;
                  this.pressure = pressure;
                  messurementsChanged();
              }
      }

       

    • Specific observer: CurrentConditionDisplay, ForecastDisplay
      public interface IDisplayElement
      {
          void Display();
      }
      public class CurrentConditionDisplay : IObserver, IDisplayElement
      {
          private float temperature;
          private float humidity;
          private float pressure;
          private ISubject weatherData;
          public void Display()
          {
              Console.WriteLine ($ " bulletin board current weather 1 => temperature:} {temperature, humidity: {} humidity, pressure: pressure {} " );
          }
      
          public void Update(float temperature, float humidity, float pressure)
          {
              this.temperature = temperature;
              this.humidity = humidity;
              this.pressure = pressure;
              Display();
          }
          public CurrentConditionDisplay(ISubject weatherData)
          {
              this.weatherData = weatherData;
              weatherData.RegisterObserver(this);
          }
      }
      public class ForecastDisplay : IObserver, IDisplayElement
      {
          private float temperature;
          private float humidity;
          private float pressure;
          private ISubject weatherData;
          public void Display()
          {
              Console.WriteLine ($ " bulletin board weather for tomorrow 2 => temperature:} {temperature, humidity: {} humidity, pressure: pressure {} " );
          }
      
          public void Update(float temperature, float humidity, float pressure)
          {
              this.temperature = temperature;
              this.humidity = humidity;
              this.pressure = pressure;
              Display();
          }
          public ForecastDisplay(ISubject weatherData)
          {
              this.weatherData = weatherData;
              weatherData.RegisterObserver(this);
          }
      }
    • Test code and operating results
      static void Main(string[] args)
      {
          WeatherData weatherData = new WeatherData();
          Console.WriteLine ( " -------- ------- observer added a bulletin board " );
          CurrentConditionDisplay display1 = new CurrentConditionDisplay(weatherData);
          weatherData.SetMessureMents(10, 20, 30);
          Console.WriteLine ( " -------- ------- bulletin board observer added 2 " );
          ForecastDisplay display2 = new ForecastDisplay(weatherData);
          weatherData.SetMessureMents(15, 25, 35);
          Console.WriteLine ( " -------- ------- bulletin board observer 1 Exit " );
          weatherData.RemoveObserver(display1);
          weatherData.SetMessureMents(19, 29, 39);
      }
  • Advantages :
    • Specific topics and specific observers are loosely coupled. Since the theme (Subject) interfaces depends only on the observer (Observer) interface, so just know that it's a specific theme observer is an instance of a class that implements the observer (Observer) interface, but does not need to know specifically which class. Also, since the observer depends only on the topic (Subject) interface, so just know that it depends on the specific observer theme is to achieve the theme (subject) instance of a class interface, but does not need to know specifically which class.
    • Observation mode to meet the "open - closed principle." Theme (Subject) interfaces depends only on the observer (Observer) interfaces, so that we can make creating specific theme category is only dependent on the observer (Observer) interfaces, so if you add a new realization observer (Observer) Interface classes, without having to modify the code to create a specific topic of the class. Also, create a concrete observer class depends only on the topic (Observer) interfaces, if adding a new realization topic (Subject) class interface, there is no need to modify the code to create a concrete observer class.
  • Source Address: https://github.com/DonyGu/DesignPatterns

Guess you like

Origin www.cnblogs.com/donyblog/p/11370690.html