close
 

This tutorial is about observer pattern, which is my thoghts after watching the tutorial created by Christopher Okhravi.

IObserverable  is something that is being observed by IObserver, where I represents interface.

IObserver wants to know if IObserverable has changed

Poll: IObserver keeps asking “Have you change?”every second.

Push: Instead of the above bad idea, IObservable pushes message automatically whenever its status has changed. 

0.* means a IObservable may have many IObservers at the same time.

new ConcreteObserver(Concreteobservable instance): The red arrow shows that

ConcreteObserver can access ConcreteObservable instance whenever it calls Update() method.

image

IObserver portion:

   1: public interface IObserver
   2: {
   3:     void Update();
   4: }

IObservable portion:

   1: interface IObservable
   2: {
   3:     void Add(IObserver o);
   4:     void Remove(IObserver o);
   5:     void Notify();
   6: }

image

WeatherStation portion:

Since WeatherStation class inherits from IObservable, it needs to implement three methods, including Add(), Remove(), and Notify().

   1: public class WeatherStation : IObservable
   2: {
   3:    IList<IObserver> observers = new List<IObserver>();
   4:  
   5:    public void Add(IObserver o)
   6:    {
   7:        observers.Add(o);
   8:    }
   9:  
  10:    public void Notify()
  11:    {
  12:        foreach(IObserver o in observers)
  13:        {
  14:            o.Update();
  15:        }
  16:    }
  17:    public int GetTemperature()
  18:    {
  19:        Random rnd = new Random((int)DateTime.Now.Ticks & 0x0000FFFF);
  20:        return rnd.Next(0, 30);
  21:    }
  22:    public void Remove(IObserver o)
  23:    {
  24:        observers.Remove(o);
  25:    }
  26: }

PhoneDisplay portion:

   1: public class PhoneDisplay : IObserver, IDisplay
   2: {
   3:    WeatherStation station;
   4:    int temperature;
   5:    public PhoneDisplay(WeatherStation _station)
   6:    {
   7:        this.station = _station;
   8:    }
   9:  
  10:    public void Display()
  11:    {
  12:        Console.WriteLine("Current Temperature is {0} degrees Celsius", temperature);
  13:    }
  14:  
  15:    public void Update()
  16:    {
  17:        temperature = station.GetTemperature();
  18:        Display();
  19:    }
  20: }

Client portion:

PhoneDisplay phone1 = new PhoneDisplay(station):

We pass station to PhoneDisplay constructor, because display somehow must be able to look at the weather station.

station.Add(phone1): The purpose of doing this is“Whenever Notify() is called, phone1 will receive the message from the station.”(Push instead of Poll)

   1: static void Main(string[] args)
   2: {
   3:    // One ConcreteObservable
   4:    WeatherStation station = new WeatherStation();
   5:  
   6:    // Two ConcreteObservers
   7:    PhoneDisplay phone1 = new PhoneDisplay(station);
   8:    PhoneDisplay phone2 = new PhoneDisplay(station);
   9:  
  10:    // ConcreteObservable adds two ConcreteObservers
  11:    station.Add(phone1);
  12:    station.Add(phone2);
  13:  
  14:    // Send message from ConcreteObservable
  15:    station.Notify();
  16:    Console.ReadKey();
  17: }

Testing results:

image

arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()