Observer Design Pattern in C#!
2024-07-21 02:19:44
供稿:网友
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 notifications. the design pattern suggests that these bos need not to know about the existence of each other or anyone else for that matter except 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 calls the methods (also called callback) on each of the clients.
what does this have to do with c#?
well,c# provides built in notification mechanism through delegate functions.this article will illustrate the same.before you really start reading this article any further,please refer to the article on delegates
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 type object and 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 called eventargs.this is important .so we will first define a class called cevent which is derived from eventargs. 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 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 defintion of delegate method that server is supposed to call then in the constructor ,it adds this method(pointer to function ,for c++ analogy) to the list of callback methods of server by calling
m_rccoll.onadditem += new ccollection .addnewitem (onadditem);
note the "+=" operator .
following is the client class.
class businessobject
{
ccollection m_rccoll;
string m_bo;
public void onadditem(object theobj, citem theitem)
{
console.writeline ("[{2}]>>>new item added to the collection: key = {0} value = {1}",
theitem.m_cszkey,theitem.m_lvalue,m_bo);
}
public businessobject( ccollection thecoll,string thename)
{
m_bo = thename;
m_rccoll = thecoll;
m_rccoll.onadditem += new ccollection .addnewitem (onadditem);
}
}
to add all up here is the resultant code:
namespace ws
{
using system;
class testnotify
{
public static void main()
{
ccollection thecoll = new ccollection() ;
businessobject thebo1 = new businessobject (thecoll,"wo");
businessobject thebo2 = new businessobject (thecoll,"lis bo");
thecoll.additem ("ashish",6674);
}
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;
}
}
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);
}
}
}
class businessobject
{
ccollection m_rccoll;
string m_bo;
public void onadditem(object theobj, citem theitem)
{
console.writeline ("[{2}]>>>new item added to the collection: key = {0} value = {1}",
theitem.m_cszkey,theitem.m_lvalue,m_bo);
}
public businessobject( ccollection thecoll,string thename)
{
m_bo = thename;
m_rccoll = thecoll;
m_rccoll.onadditem += new ccollection .addnewitem (onadditem);
}
}
}
}
注册会员,创建你的web开发资料库,