Click here to Skip to main content
15,885,537 members
Articles / Programming Languages / C#

Implementing Observer pattern in .NET

Rate me:
Please Sign up or sign in to vote.
2.74/5 (21 votes)
28 Apr 2007CPOL5 min read 45.8K   72   35  
The observer pattern should be used whenever one or more objects (observers) are interested to know the states of subject
using System;
using System.Collections.Generic;
using System.Text;

namespace ObserverPattern
{
    class CurrencyRateChangeObserver
    {
        //Reference to the watcher we�re working with
	    CurrencyRateWatcher  oCurrencyRateWatcher;
	
	    public CurrencyRateChangeObserver (CurrencyRateWatcher  cwr)
        {
            oCurrencyRateWatcher = cwr;
	
            // set up the delegate
            oCurrencyRateWatcher.OnRateChange += new CurrencyRateWatcher.CurrencyRateChange(RateHasChanged);
        }

        //The callback method
        public void RateHasChanged (object sender, CurrencyRateChangeInfo oInfo)
        {
           Console.WriteLine("Currency: �{0}� has new value �{1}� and the change Time is {2}", oInfo.sCurrencyName, oInfo.dNewRate, oInfo.tChangeTime);
        }



    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Bangladesh Bangladesh
This is S.M. Rabiul Islam from Bangladesh, I have been working as a software engineer here in Bangladesh at offshore software development house

Comments and Discussions