close

Today I reveiewed Observer Pattern – Design Pattern (Using C#) and have finished watching Observer Design Pattern tutorial.

As usual, I implemented the example in C# instead of JAVA.

ISubject (Observerble) Portion:

 

 

 

   1: interface ISubject
   2: {
   3:     void Register(IObserver o);
   4:     void Unregister(IObserver o);
   5:     void NotifyObserver();
   6: }

IObserver Portion:

 

   1: interface Observer
   2: {
   3:     void Update(double ibmPrice, double appPrice, double googPrice);
   4: }

StockGrabber Portion:

 

   1: class StockGrabber : ISubject
   2: {
   3:     IList<IObserver> observers;
   4:     private double ibmPrice;
   5:     private double appPrice;
   6:     private double googPrice;
   7:     public StockGrabber()
   8:     {
   9:         observers = new List<IObserver>();
  10:         ibmPrice = 0;
  11:         appPrice = 0;
  12:         googPrice = 0;
  13:     }
  14:     public void NotifyObserver()
  15:     {
  16:         foreach(IObserver observer in observers)
  17:         {
  18:             observer.Update(ibmPrice, appPrice, googPrice);
  19:         }
  20:     }
  21:     public void setIBMPrice(double ibmPrice)
  22:     {
  23:         this.ibmPrice = ibmPrice;
  24:         NotifyObserver();
  25:     }
  26:     public void setAPPPrice(double appPrice)
  27:     {
  28:         this.appPrice = appPrice;
  29:         NotifyObserver();
  30:     }
  31:     public void setGOOGPrice(double googPrice)
  32:     {
  33:         this.googPrice = googPrice;
  34:         NotifyObserver();
  35:     }
  36:     public void Register(IObserver newObserver)
  37:     {
  38:         observers.Add(newObserver);
  39:     }
  40:  
  41:     public void Unregister(IObserver deleteObserver)
  42:     {
  43:         int observerIndex = observers.IndexOf(deleteObserver);
  44:         Console.WriteLine("Observer {0} deleted", observerIndex+1);
  45:         observers.RemoveAt(observerIndex);
  46:     }
  47: }

 

 

StockObserver Portion:

   1: class StockObserver : IObserver
   2: {
   3:     double ibmPrice;
   4:     double appPrice;
   5:     double googPrice;
   6:     static int observerIDTracker = 0;
   7:     ISubject stockGrabber;
   8:     int observerID;
   9:  
  10:     public StockObserver(ISubject stockGrabber)
  11:     {
  12:         this.stockGrabber = stockGrabber;
  13:         this.observerID = ++observerIDTracker;
  14:         Console.WriteLine("New Observer {0}", this.observerID);
  15:         stockGrabber.Register(this);
  16:     }
  17:     public void Update(double ibmPrice, double appPrice, double googPrice)
  18:     {
  19:         this.ibmPrice = ibmPrice;
  20:         this.appPrice = appPrice;
  21:         this.googPrice = googPrice;
  22:         printThePrices();
  23:     }
  24:     private void printThePrices()
  25:     {
  26:         Console.WriteLine("#{0} \n IBM:{1}\n Apple:{2}\n Google:{3}", 
  27:             observerID, this.ibmPrice, this.appPrice, this.googPrice);
  28:     }
  29: }

Client Portion:

   1: static void Main(string[] args)
   2: {
   3:    StockGrabber stockGrabber = new StockGrabber();
   4:    StockObserver observer1 = new StockObserver(stockGrabber);
   5:    stockGrabber.setIBMPrice(197.00);
   6:    stockGrabber.setAPPPrice(677.60);
   7:    stockGrabber.setGOOGPrice(676.40);
   8:  
   9:    StockObserver observer2 = new StockObserver(stockGrabber);
  10:    stockGrabber.setIBMPrice(197.00);
  11:    stockGrabber.setAPPPrice(677.60);
  12:    stockGrabber.setGOOGPrice(676.40);
  13:  
  14:    stockGrabber.Unregister(observer2);
  15:    stockGrabber.setIBMPrice(100);
  16:    stockGrabber.setAPPPrice(500.60);
  17:    stockGrabber.setGOOGPrice(600.40);
  18:  
  19:    Console.ReadKey();
  20: }

C# Trackbar Application Using Observer Pattern

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

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