Introduction
This article provides a basic template for implementing the Observer Pattern using an event
. This article aims to concisely share with you how to use events to implement the Observer Pattern by presenting the bare minimum code needed to do so. Please feel free to modify this template to suit your projects’ requirements.
To enhance your learning experience, you can download a Console project and experiment with it as you read this article from here.
Background
The Observer Pattern is frequently implemented in one of two ways:
- We make our own Subject/Observer abstractions and implementations (e.g., creating,
ISubject
, IObservable
, and relevant derived classes) - We let our Subject/Observer inherit from
IObservable<T>
and IObserver<T>
Although these are the most popular implementations, we may also consider another implementation: using events
.
Using the Code
There are two essential classes: the Subject
class and Observer
class.
Subject
The subject
contains the event field and can raise the event.
public class Subject
{
public event EventHandler eventHandler;
public void NotifyObservers()
{
if (eventHandler != null)
{
eventHandler(this, EventArgs.Empty);
}
}
}
Notes
- Contains the event:
public event EventHandler eventHandler;
This is the glue that holds the subject
and the observer
s together - Raises the event:
eventHandler(this, EventArgs.Empty);
All observers subscribed to this event will then be notified that this event has been raised
Observer
The observer
can subscribe and unsubscribe to the subject
. It also attaches a handler to the subject
's event.
public class Observer
{
Subject subject;
public Observer(Subject subject)
{
this.subject = subject;
}
public void Subscribe()
{
subject.eventHandler += DoSomething;
}
public void UnSubscribe()
{
subject.eventHandler -= DoSomething;
}
private void DoSomething(object sender, EventArgs e)
{
Console.WriteLine("This Observer instance has received
a notification from its associated Subject.");
}
}
Notes
- Contains the event handler:
private void DoSomething(object sender, EventArgs e) {...}
This is called when Subject's eventHandler
is raised. - Subscribes by attaching a handler to
Subject's eventHandler: subject.eventHandler += DoSomething
;
We can also subscribe multiple times. - Unsubscribes by detaching the handler from
subject's eventHandler: subject.eventHandler -= DoSomething
;
Main
We run our Observer Pattern in the Main
method:
class Program
{
static void Main(string[] args)
{
Subject subject = new Subject();
Observer observer1 = new Observer(subject);
Observer observer2 = new Observer(subject);
observer1.Subscribe();
observer2.Subscribe();
subject.NotifyObservers();
observer1.UnSubscribe();
subject.NotifyObservers();
Console.ReadKey();
}
}
Notes
- Since we have unsubscribed
observer1,
our console outputs the statement "This Observer instance has received a notification from its associated Subject." three times instead of four times.
Further Reading
Thanks for reading! Hopefully, this article helped you learn another way of implementing the Observer Pattern in C#. If you have any comments, please feel free to share it below.