Observer Pattern in C#

Observer Pattern

Download code from here.


Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. It is a subset of publish-subscribe pattern. We use observer as a view in MVC pattern. C# is very much suited for this language with the power of events and delegates.


We always need loose coupling between objects. Observer pattern promotes loose coupling. Subject doesn’t depend on the observer but they are registered/unregistered to the subject.

Consider the football game in which all players (as well as linesman/referee) need to move and take position according to the position of the ball. There must present some sort of mechanism which send message to all observers about the position change activity of the ball. Yes, observer pattern is the useful one.


Subject is the one which register/unregister the observer and notifies the observers about the state. Observers have reference of subject and get updated as subject changes. Note the comment ‘foreach o in obserers’ which implies all registered observers get updated.


Full code can be downloaded from here.

I created the simple example of Observer Pattern with the concept of football game. I have used event for registering/unregistering observers. I have used events for registering/unregistering observer.

I created an IPerson interface which is our observer. It is the one which is registered to the subject.

public interface IPerson


void UpdatePosition();

Position Position { get; set; }


And our subject is the Ball abstract class. It is abstract because I think I can extend it to any other balls like FootBall, VolleyBall etc. It has the AttachObserber and DettachObserver functions which registered and unregistered the observer. Whenever there is change in the position of the ball, it uses Notify function to notify all registered observers.

public abstract class Ball
public Ball(double size, string color)
_size = size;
_color = color;
_persons = new List(0);

public void AttachObserver(IPerson person)
_positionChanged += person.UpdatePosition;

public void DettachObserver(IPerson person)
_positionChanged -= person.UpdatePosition;

public void DettachAll()
foreach (IPerson p in _persons)
_positionChanged -= p.UpdatePosition;

public Position Position
get { return _position; }
_position = value;
Console.WriteLine("Current football position: {0}, {1}", _position.XPos, _position.YPos);

public void NotifyPlayers()
if (_positionChanged != null)

private Position _position;
private double _size;
private string _color;
private List _persons;
private NotifyBallObserverEventHandler _positionChanged;


Observer defines a much decoupled interface that allows for multiple receivers to be configured at run-time. Mediator pattern also provides the mediator object which encapsulates the communication between objects. But observer provides the different mechanism where all observers get updated when subject changes.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s