Observer Design Pattern

A couple of months back Instagram introduced IGTV app.

Now Instagram had notified all its users about this new app.

And as soon as the users got the notification, they went to the app and checked out what exactly Instagram has come up with.


Observable: One who pushes notification to all its observers.

Observer: One who keep an eye on if there are any changes or anything newly added.

In our current example:

Instagram : Observable

Users : Observers

(DP: Design Pattern)

This DP is one of the behavioral DP.

The state of observable is what the observer is interested in.
Class Diagram of Observer Design Patten

Here we expect a user to get the updates which IGTV has brought.

Now as a user of IGTV every user will run the same getUpdate() method.

Let’s create one concrete User.

Here every user will connect with the app in order to get the updates.

Now let’s create an interface containing methods specific to the app.

The IGTV will notify all the user as one by one about the updates.

Here apart from overriding the IApp methods the class has one more method mentioning all the updates.

Note: We do not take the object in the IApp interface but in the IGTV interface.

Here the app has users and on any state change in the app, it calls notify method on all its users.

Catch in this DP is that every user has a reference to the concrete app (AppImp) and not its interface (IApp).

That’s when the user on being notified, calls the internal methods (what has been changed) of the app.

Final words:

Observer DP ensures that when one object changes its state all the dependent objects are updated automatically.


Thanks for reading. I hope it helps.