Today I would like to present the Observer pattern from the familiy of the behavioral patterns to you. From my point of view it is one of the most important and most used patterns and should be part of the basic knowledge of every good programmer. Some of you may want to say : “Hey why do you spent time in writing this ? Java and .NET have delegates and events so you most commonly don’t have to implement observers on your own.” You are right ! But I think it is very important to understand the technology behind it and so I want to give the beginners of you a crash course.
To warm up a little, let’s do a small example:
Let’s imagine we are sitting at a controlpanel and our job is to supervise the location of some trains. That would be an enourmous work to always ask the trains for their location, wouldn’t it ? So why don’t we turn the tides ? Let the train do the job and let him notify us if his location has changed. All we have to do is to tell him that they should notify us in time. The Advantage with that is, that the train is able to notify not just you but also everyone who has told him to do so. So all of your teammates are able to get the train’s position, too.
So how will it look like in an objectmodel ?
Well it’s easier than you think. Imagine yourself as an observer who is able to register at a train. The train, which is our subject, has a list of observers (you and your teammates) and will notify you all in case of a positionchange or something else. Well basically that’s all you have to know about.
Lets create some classes :
At first we need a class that represents you. Therefore we implement an Interface called ITrainMonitor which contains all relevant methods (that’s Update()). Of course we have to write an implementation of that method but we will come to this later.
After that we need our subject we want to observe. Our Train has some Attributes like “Speed” or “Position” but the methods for our architecture are more interesting for us. Those are :
- Register(Observer) -> to let the train notify us in time
- UnRegister(Observer) -> to tell it to stop notifying us
- Notify() -> informs us observers that the status of the train has changed
Let’s take a look at the UML diagram

So now we need what to do, let’s get hand on the code :
Here’s the implementation of our TrainMonitor
public class TrainMonitor : ITrainMonitor
{
private string _name;
// <summary>
// Defaultconstructor
// </summary>
//
<param name="name">Name of the monitorer</param>
public TrainMonitor( string name )
{
_name = name;
}
// <summary>
// Name of the Monitorer
// </summary>
public string Name
{
get { return _name; }
}
// <summary>
// Train has changed its state so update the Monitor too
// </summary>
//
<param name="train">Train that has changed state</param>
public void Update( Train train )
{
Console.WriteLine( "Monitor {0} was notified that {1} changed location to {2} \n", _name, train.Name, train.Location );
}
}
our train looks like this now :
public abstract class Train
{
private string _name;
private string _location;
private List<ITrainMonitor> _observers;
protected Train( string name )
{
_observers = new List<ITrainMonitor>();
_name = name;
}
//Register a new Observer
public void RegisterObserver( ITrainMonitor monitor )
{
_observers.Add( monitor );
}
//Unregister an Observer
public void UnRegisterObserver( ITrainMonitor monitor )
{
_observers.Remove( monitor );
}
//Notify all Observers about made changes
public void Notify()
{
foreach ( ITrainMonitor observer in _observers )
observer.Update( this );
}
//Location of a train , setting will call up an Update on all Observers
public string Location
{
get { return _location; }
set
{
_location = value;
Notify();
}
}
public string Name
{
get { return _name; }
}
}
A short scenario for testing
public static void Main(string[] args)
{
// A sample scenario
PassengerTrain passTrain = new PassengerTrain( "ICE" );
GoodsTrain goodsTrain = new GoodsTrain( "Vegetables" );
TrainMonitor monitor1 = new TrainMonitor( "Peter" );
TrainMonitor monitor2 = new TrainMonitor( "Simon" );
passTrain.RegisterObserver( monitor1 );
passTrain.RegisterObserver( monitor2 );
goodsTrain.RegisterObserver( monitor2 );
passTrain.Location = "Cologne";
goodsTrain.Location = "Bonn";
passTrain.UnRegisterObserver( monitor2 );
passTrain.Location = "Hamburg";
goodsTrain.Location = "Frankfurt";
Console.Write("Press any key to continue . . . ");
Console.ReadKey( true );
}

Well that wasn’t that hard, was it ? Of course you can download the the whole example from my SVN Repository and play with it. The common Observer Pattern UML diagram looks as follows :
Source : http://de.wikipedia.org
I hope I was able to get you a little bit into the Observer Pattern from the GoF
Click HERE to see the complete sourcecode
Deutsch
Englisch


Leave a Reply