Observer Design Pattern in C#!

原创 2001年10月25日 16:36:00

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.

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:


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

Note the "+=" operator .

Following is the client class.

To add all up here is the resultant code:


Observer Design Pattern

  • kissjob
  • kissjob
  • 2011年07月11日 15:32
  • 260

设计模式之观察者模式 (Observer Design Pattern)

  • suifeng3051
  • suifeng3051
  • 2016年04月27日 19:32
  • 939

Design Pattern - Observer(C#)

Definition Define a one-to-many dependency between objects so that when one object changes state, a...
  • chimomo
  • chimomo
  • 2010年12月03日 09:42
  • 29450

Observer Design Pattern in C#!

  • zgqtxwd
  • zgqtxwd
  • 2008年04月24日 12:59
  • 83

[Design Pattern]Observer Pattern

观察者模式(代表人物MVC)有一个模式可以帮你的对象知悉现况,不会错过该对象感兴趣的事。对象甚至在运行时可决定是否要继续被通知。定义 观察者模式定义了一系列对象之间的一对多关系。 当一个对象改变状态,...
  • xy_cpp
  • xy_cpp
  • 2017年11月16日 09:42
  • 62

Design Pattern --------Observer pattern

Below is an example that takes keyboard input and treats each input line as an event. The example is...
  • dragonsdg3
  • dragonsdg3
  • 2012年07月01日 16:18
  • 201

设计模式 - 观察者模式(Observer Pattern) 详解

观察者模式(Observer Pattern) 详解 本文地址: 版权...
  • u012515223
  • u012515223
  • 2014年05月22日 14:37
  • 2972

理解观察者模式(Observer Pattern)必看的文章

1.       说明:观察者模式是软件设计的一种模式,也叫做发布/订阅模式。日常生活中基本上都有用到它。比如使用MSN当你的一个好友上线时,你就会收到上线通知;开心网当某个人转帖后,他的好友就都可以...
  • dujingjing1230
  • dujingjing1230
  • 2009年08月10日 14:56
  • 3488

C++版的Head First Design Pattern

       前 些天从sourceforge上down下一个C++版的Head First Design Pattern,一直都没有仔细看过,也不知道HeadFirstDesignPattern是一...
  • iinel
  • iinel
  • 2007年11月16日 10:13
  • 1578

Design Pattern—— Observer Pattern 观察者模式

Observer Pattern 观察者模式: 观察者模式定义了一种一对多的依赖关系,
  • u013147914
  • u013147914
  • 2014年04月14日 19:58
  • 318
您举报文章:Observer Design Pattern in C#!