Moving on with the concept of design patterns, we will be discussing the concept of Observer design pattern. Observer pattern is the type of behavioral design pattern, which defines how the components or objects in the system interact with each other or send communication messages.

The basic concept of this pattern is to design a system with one component(say a master component) notifying its change in state to other components of the system, which are dependent on any change in the master component want to get notified.

According to the GoF’s patterns, it is defined as

” Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically “.

This pattern is same as the concept of events and delegate, in which an event when fired, notifies the components who are registered to it using the delegates.

A real world example

One of the simplest example that can be considered is this wordpress site, where users post their articles on blog. An interested reader may not be able to visit a blog regularly, to check for a new post. So what this site does is, allow a user to subscribe to the bloggers’ posts. So whenever a blogger posts any article, the people who have subscribed to the blogger will get to know about the new post by email notifications.

Let’s get technical

We will be creating a console based application, with following four components to run this pattern. Also we will be using the following terminology interchangeably, to discuss it :

Blog Writer, who posts the blogs — As Publisher

Blog Reader, who wants to subscribe for the new post notifications — As Subscriber

1. INotificationService : An interface with the functions used by the publisher to manage the subscribers, who would like to receive notification) and send them the notifications.

2. BlogWriter : A concrete class implemented by the publisher or you can say a blog writer managing his list of the subscribers and way of notifying his users.

3. IBlogReaders : An interface used by the subscribers to receive the notifications

4. BlogReaders : A concrete implementation of the above interface,

So first, we will be defining a notification service INotificationService, which will be used by the publisher to add/remove or notify the subscribers. We will be having three methods i.e. AddSubscriber, RemoveSubscriber & NotifySubscribers.

Next we will be defining a way the subscribers will receiver their notifications i.e. IBlogReaders. So our code will look like the following :

Next, our publisher will be implementing the service for notification, or you can say, manage his subscribers, as per his requirements. So our code will be as following :

As we can see here, the notification service is adding the type of the subscribers, who are implementing the interface IBlogReaders,which means, it is adding subscribers, who have a way of receiving notifications through this interface.

So between a publisher and the subscriber, the INotificationService & IBlogReaders act as the intermediates or the co-ordinators. INotificationService from the publisher side and IBlogReader from the subscriber side. The NotifyReaders method in above code, performs this job,

Finally, our subscriber will be implementing the IBlogReader,to manage themselves or perform what they would like to, whenever the receive any notifications. So our code will be as following :

Here, we will be simply writing the out put on the console.

So basic setup is done. Now its time for the blog writer to write posts’ and publish it. So our code will be like the following :

So we add the subscribers to the list and call the NotifyReadersmethod, with the notification message. So calling the this method results in call to the NotificationReceivedmethod of the subscribers (see the image previous to above one). A single method call to NotifyReaders, all the subscribers are notified by it. Run the application and see the output.

So, we could have remove any of the subscriber from the list as per our need, and the subscriber will no longer receive the notification.