您的位置:首页 > 编程语言 > C#

Observer Design Pattern in C#!

2008-04-24 12:59 501 查看
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
{
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: