Observer Design Pattern

Tushar Kumar
4 min readMay 19, 2021

Let’s first discuss What is the intent of Observer Design Pattern and What kind of problems it help us to solve.

Let’s suppose we have an entity A which maintains a certain state. By state I mean certain properties which has some values(example: Age of a person). These set of properties are expected to change at some point of time. There are other bunch of entities whose requirement is to get the notification/updated values as soon as the state of our entity A changes. To solve this type of problem, we use the Observer Design Pattern.

Here’s what Wikipedia says about Observer Design Pattern

The observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

Here the entity A which maintains a state which other entities want is called as a Subject and the entities which want the notification/updated value is called as the Observer.

As mentioned in the definition that the Subject,which is also called as the Observable(the one that can be/is observed), maintains a list of dependents called as Observers. This list is maintained in order to know which objects do we need to notify about our updated values. This brings us to our next segment which explains us how does this Observer Pattern works

In Observer Design Pattern, we generally have a Subject which holds a set of states which other objects wants(called as Observers). In order to have the knowledge as to which all objects the Subject needs to notify, the Subject maintains a list of Observers with itself. Anytime the state changes in the Subject, it looks at this list and broadcast the update event to all the participants of that List. The Subject can choose any data structures according to their convenience to maintain the observer list.

Let’s Understand this with the help of an example:

In Quora, let’s suppose we have a page which post stories about various Food recipes. There are bunch of People which want to get the notification as soon as the owner of the Page posts a new recipe. In order to achieve this behavior we are going to use Observer Pattern.

Here the Quora Page is our Subject and the Story is the state which this entity maintains. The Peoples which want the notification/updated values are the Observers

Here is the simple implementation of our example using Observer Pattern

The above interface is implemented by our Subject. It contains 5 methods as contract:

  1. follow(IObserver observer): This method helps to register an Observer with the Subject
  2. unfollow(IObserver observer): This method helps to de-register an Observer with the Subject
  3. notifyObservers(): This method helps to notify all the observers registered with the Subject
  4. getState(): Returns the value of the state maintained by the Subject
  5. setState(String story): Here story is our state. This method will be called to change the value of our state.

The above class is the concrete implementation of our ISubject interface. The class as you can observe maintains two properties:

  1. String story: This is the state which this class maintains
  2. List<IObserver> observers: This maintains the list of observers who are waiting to be notified for the state change

Most of the method implementation are self-explanatory but there are two methods which we look at closely:

  1. void notifyObservers(): This method will notify all the observers about the state change. As you can see in the implementation , we iterate through our observer list and call the update() function of the Observer
  2. void setState(String story): This method will change the state. If you look closely at the method, it is also calling the notifyObservers() internally. This means that as soon as the state is changed, all the Observers will be duly notified

This the Observer interface which all the observer will implement. It has only one method called as update() which will be used to implement the update strategy. If you can remember this is the same method which the Subject calls in the notifyObservers() method.

The Person class is the concrete implementation of our IObserver interface. The class has a parameterized constructor which takes ISubject as a parameter. This is done in order to get the updated value of the state.

The update() functions which is called from the Subject fetches the updated value.

This is the Driver class of our application. We do the following in our Driver class:

  1. Declare the subject in Line 4
  2. Declare the Observer at Line 5 as pass the Subject as the parameter
  3. We use the Subject to register the Observer at Line 6
  4. We call the setState() method of the Subject to change the state at Line 7
  5. The state will be changed and it will internally call the notifyObservers() method which will notify the registered Observers
  6. We eventually de-register our Observer from the Subject at Line 8

Hope you liked this article and understood the Observer Design Pattern. If you would like to download the code, you can download it from the following link:

Github Link:

https://github.com/officialtushark/Design-Patterns.git

--

--