|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhile I was developing a new web application in our company, I found that every entity in the system had some shared fields that held data about when and who saved that entity into the system. Basically, those fields were
In this article, I will show how to build a simple user control that implements the observer pattern to handle updates and pass notifications around, which is done in order to update a set of objects when some important event has occurred. BackgroundThe observer pattern is categorized under Behavioral Patterns. Behavioral Patterns are those patterns which are concerned with algorithms and the assignment of responsibilities between objects. Behavioral patterns describe not just patterns of objects or classes, but also the patterns of communication between them. So, What is the Observer Pattern?The observer design pattern should, “Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.” What is the Subject? What is the Observer?Subject: the object which will frequently change its state and upon which other objects depend. Observer: the object which depends on a subject and updates according to its subject's state. Here’s how it works: an observer signs up to receive notifications of changes to the subject.
A second observer can register itself, too. In fact, a subject may have any number of dependent observers. Observers are not aware of the presence of each other.
When a certain event occurs, all observers are notified.
An observer can unregister itself from a subject so that it will never receive notifications from the subject.
With this generic way of communicating between the subject and observers, collaborations can be built dynamically instead of statically. The code is now much more separate, and thus easier to maintain and reuse. There is no direct dependency between subject class and observer class. Using the Code
In our example, Implementing Subject ClassYou can implement the public interface ISubject
{
List<IObserver> ObserversList {get;}
void AttachObserver(IObserver observer);
void DeAttachObserver(IObserver observer);
void NotifyObservers();
}
Implementing the Observer Classpublic interface IObserver
{
void Updateobject(ISubject subject);
}
The observer class is very simple and contains a method that allows subjects to notify it. All we need to know is how to implement #region ISubject Members
public List<ObserverTutorial.App_Code.IObserver> ObserversList
{
get { return this.m_ObserversList; }
}
public void AttachObserver(ObserverTutorial.App_Code.IObserver observer)
{
this.ObserversList.Add(observer);
}
public void DeAttachObserver(ObserverTutorial.App_Code.IObserver observer)
{
this.ObserversList.Remove(observer);
}
public void NotifyObservers()
{
foreach (App_Code.IObserver observer in this.ObserversList )
{
observer.Updateobject(this);
}
}
#endregion
Note that the protected void btnOk_Click(object sender, EventArgs e)
{
bool result = SaveEntityData();
/// After Subject object data is saved in a state notify observers
if (result)
{
this.NotifyObservers();
}
}
In the ISubject Subject = this.Parent as ISubject;
if (Subject != null)
{
Subject.AttachObserver(this);
}
Points of InterestThe Microsoft .NET Framework defines the notion of delegates and events to accomplish the observer role. Therefore you would rarely implement the observer pattern explicitly in .NET, but should use delegates and events instead. I attached an implementation of this sample using delegates and events, too. History
|
||||||||||||||||||||||