Observer Design Pattern in C #!

Source: Internet
Author: User

 

This article describes a very important and one of the powerful tools in C #.

This is based on a famous design pattern called Observer. observer design pattern dictates what is called as subscriber-publisher paradigm. an example will make it clear. lets say we are building a 3 tier architecture where there are several middle layer business objects. BO_1 BO_2 BO_3 etc. all these business objects provide some services and fire some communications. the design pattern suggests that these BOs need not to know about the existence of each other or anyone else for that matter should t one central controller or manager. the manager is responsible for getting events from different BOs and then route those events to interested clients. for example BO_1 may be interested in events from BO_2.It needs to register for events with central Manager CM. CM is responsible for sending events to all interested clients. when BO_1 detects some change in its state it notifies CM and CM walks through a list of clients which had registered for those events and CILS the methods (also called callback) on each of the clients.

What does this have to do with C #?

Well, C # provides built in notification mechanic through delegate functions. This article will adjust strate the same. Before you really start reading this article any further, please refer to the article onDelegates 

Once you know what delegates are understanding this article and hence a powerful paradigm is a piece of cake. To recall, delegates are equivalent to function pointers.

Lets take a scenario. suppose we have a collection which has several methods including AddItem (). lets say there are several Business objects which want to get notified whenever a new item is added to collection so that they can do some things like update their internal data structures.

In our example we will need to have a Collection class which we will call CCollection. in addition to that we will have another class BusinessObject which will have a CCollection object in it and will be interested in knowing when the collection changed.

So lets try to design this in C #. we first need to define CCollection class which will have a method called AddItem which will be called to add a new item to the collection. the AddItem takes a key and a value as the parameter. so whenever a AddItem is called on Collection, all clients need to know about this. in other words the collection class needs to fire a notification to all its clients. how does It do so? The answer lies in delegate methods. First of all the Collection needs to publishes or define a method prototype which has two parameters: first parameter is of the typeObjectAnd the second parameter needs to be the data that is passed along with the notification. The second parameter needs to be a class derived from a system class calledEventArgs. This is important. So we will first define a class called CEvent which is derived fromEventArgs. CCollection class sends this object along with the notification. Here is the definition of CEvent.

class CItem:EventArgs
{
public readonly string m_cszKey;
public readonly long m_lValue;
public  CItem(string strKey,long lVal)
{
m_cszKey = strKey;
m_lValue = lVal;
}
}

Note that the class has only two variables which are readonly which mean they can be changed one time. the collection class passes the key and the value to its clients. so in the AddItem method of CCollection class we will call the OnAddItem which is implemented by the clients who want to get notifications. following is the code for CCollection class:

 

class  CCollection
{
public delegate void AddNewItem(object theObj,
 CItem theItem);
public event AddNewItem OnAddItem;
public void AddItem(string theKey,long theVal)
{
CItem theRCItem = new  CItem (theKey,theVal);
if(OnAddItem != null )
{
OnAddItem(this,theRCItem);
}
}
}

Next we need to define a client class: As we said in the begining of this article that clients need to communicate to the server that they are interested in certain type of events. and need to pass a callback method so that server can call that method on client. how this is done is that client needs to provide an implementation of the delegate method and tell sever about that method. if you look at the code below the client BusinessObject first gives the definition of delegate method that server is supposed to call then in the constructor, it adds this method (pointer to function, for C ++ analog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.