Observer Pattern in the .NET Framework

If you are using the .NET Framework, there is no need to ever implement the Observer Pattern. Instead, you will want to use Delegates and Events.

Per Doug Purdy (Microsoft veteran) and Jeffrey Richter (Wintellect):

…note that no IObserver, IObservable, or ObservableImpl types are present in the Framework. The primary reason for their absence is the fact that the CLR makes them obsolete after a fashion. Although you can certainly use these constructs in a .NET application, the introduction of delegates and events provides a new and powerful means of implementing the Observer pattern without developing specific types dedicated to support this pattern. In fact, as delegates and events are first class members of the CLR, the foundation of this pattern is incorporated into the very core of the .NET Framework. As such, the FCL makes extensive use of the Observer pattern throughout its structure. [ref]

Here’s an example of how to implement the Observer Pattern utilizing .NET Delegates and Events:

    1 //Observer using delegates and events (C#)

    2 

    3 public class Stock

    4 {

    5     //declare a delegate for the event

    6     public delegate void AskPriceDelegate(object aPrice);

    7     //declare the event using the delegate

    8     public event AskPriceDelegate AskPriceChanged;

    9 

   10     //instance variable for ask price

   11     object _askPrice;

   12 

   13     //property for ask price

   14     public object AskPrice

   15     {

   16 

   17         set

   18         {

   19             //set the instance variable

   20             _askPrice = value;

   21 

   22             //fire the event

   23             AskPriceChanged(_askPrice);

   24         }

   25 

   26     }//AskPrice property

   27 

   28 }//Stock class

   29 

   30 //represents the user interface in the application

   31 public class StockDisplay

   32 {

   33 

   34     public void AskPriceChanged(object aPrice)

   35     {

   36         Console.Write("The new ask price is:" + aPrice + "\r\n");

   37     }

   38 

   39 }//StockDispslay class

   40 

   41 public class MainClass

   42 {

   43 

   44     public static void Main()

   45     {

   46 

   47         //create new display and stock instances

   48         StockDisplay stockDisplay = new StockDisplay();

   49         Stock stock = new Stock();

   50 

   51         //create a new delegate instance and bind it

   52         //to the observer’s askpricechanged method

   53         Stock.AskPriceDelegate aDelegate = new

   54            Stock.AskPriceDelegate(stockDisplay.AskPriceChanged);

   55 

   56         //add the delegate to the event

   57         stock.AskPriceChanged += aDelegate;

   58 

   59         //loop 100 times and modify the ask price

   60         for (int looper = 0; looper < 100; looper++)

   61         {

   62             stock.AskPrice = looper;

   63         }

   64 

   65         //remove the delegate from the event

   66         stock.AskPriceChanged -= aDelegate;

   67 

   68     }//Main

   69 

   70 }//MainClass

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s