c#에서의 observer pattern , delegate, event

http://msdn.microsoft.com/en-us/library/ms954621.aspx

IObserver and IObservable interfaces (C#)

//interface the all observer classes should implement
public interface IObserver {
  
   void Notify(object anObject);
  
}//IObserver

//interface that all observable classes should implement
public interface IObservable {

   void Register(IObserver anObserver);
   void UnRegister(IObserver anObserver);

}//IObservable

ObservableImpl Class (C#)

//helper class that implements observable interface
public class ObservableImpl:IObservable {
     
   //container to store the observer instance (is not synchronized for
         this example)
   protected Hashtable _observerContainer=new Hashtable();
  
   //add the observer
   public void Register(IObserver anObserver){
      _observerContainer.Add(anObserver,anObserver);
   }//Register
     
   //remove the observer
   public void UnRegister(IObserver anObserver){
      _observerContainer.Remove(anObserver);
   }//UnRegister

   //common method to notify all the observers
   public void NotifyObservers(object anObject) {
        
      //enumeration the observers and invoke their notify method
      foreach(IObserver anObserver in _observerContainer.Keys) {

         anObserver.Notify(anObject);

      }//foreach
     
   }//NotifyObservers

}//ObservableImpl

Observer Example (C#)

//represents a stock in an application
public class Stock:ObservableImpl {
     
   //instance variable for ask price
   object _askPrice;

   //property for ask price
   public object AskPrice {

      set {    _askPrice=value;
         base.NotifyObservers(_askPrice);
                   }//set
     
   }//AskPrice property

}//Stock

//represents the user interface in the application
public class StockDisplay:IObserver {

   public void Notify(object anObject){
      Console.WriteLine("The new ask price is:" + anObject);
   }//Notify

}//StockDisplay

public class MainClass{

   public static void Main() {

      //create new display and stock instances
      StockDisplay stockDisplay=new StockDisplay();
      Stock stock=new Stock();

      //register the grid
      stock.Register(stockDisplay);

      //loop 100 times and modify the ask price
      for(int looper=0;looper < 100;looper++) {
         stock.AskPrice=looper;
      }

      //unregister the display
      stock.UnRegister(stockDisplay);
     
   }//Main
  
}//MainClass

Observer using delegates and events (C#)

public class Stock {

   //declare a delegate for the event
   public delegate void AskPriceDelegate(object aPrice);
   //declare the event using the delegate
   public event AskPriceDelegate AskPriceChanged;

   //instance variable for ask price
   object _askPrice;

   //property for ask price
   public object AskPrice {

      set {
         //set the instance variable
         _askPrice=value;

         //fire the event
         AskPriceChanged(_askPrice);
      }
     
   }//AskPrice property

}//Stock class

//represents the user interface in the application
public class StockDisplay {

   public void AskPriceChanged(object aPrice) {
      Console.Write("The new ask price is:" + aPrice + "\r\n"); }

}//StockDispslay class

public class MainClass {

   public static void Main(){

      //create new display and stock instances
      StockDisplay stockDisplay=new StockDisplay();
      Stock stock=new Stock();
  
      //create a new delegate instance and bind it
      //to the observer's askpricechanged method
      Stock.AskPriceDelegate aDelegate=new
         Stock.AskPriceDelegate(stockDisplay.AskPriceChanged);
        
      //add the delegate to the event
      stock.AskPriceChanged+=aDelegate;

      //loop 100 times and modify the ask price
      for(int looper=0;looper < 100;looper++) {
         stock.AskPrice=looper;
      }

      //remove the delegate from the event
      stock.AskPriceChanged-=aDelegate;

   }//Main

}//MainClass

Event Pattern Example (C#)

public class Stock {

      //declare a delegate for the event
      public delegate void AskPriceChangedHandler(object sender,
            AskPriceChangedEventArgs e);
      //declare the event using the delegate
public event AskPriceChangedHandler AskPriceChanged;

      //instance variable for ask price
      object _askPrice;

      //property for ask price
      public object AskPrice {

         set {
            //set the instance variable
_askPrice=value;

//fire the event
OnAskPriceChanged();
     }
     
      }//AskPrice property
              
               //method to fire event delegate with proper name
      protected void OnAskPriceChanged() {

         AskPriceChanged(this,new AskPriceChangedEventArgs(_askPrice));

      }//AskPriceChanged

   }//Stock class

   //specialized event class for the askpricechanged event
   public class AskPriceChangedEventArgs:EventArgs {

      //instance variable to store the ask price
      private object _askPrice;

      //constructor that sets askprice
      public AskPriceChangedEventArgs(object askPrice) { _askPrice=askPrice; }

      //public property for the ask price
      public object AskPrice { get { return _askPrice; } }

   }//AskPriceChangedEventArgs

by Int32 | 2008/12/16 17:39 | c# | 트랙백(1) | 덧글(0)

트랙백 주소 : http://scilence.egloos.com/tb/1229215
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Tracked from JAVA/JSP/STR.. at 2009/02/19 12:23

제목 : 샘플 코드..
c#에서의 observer pattern , delegate, eventobserver pattern의 샘플 코드.....more

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶