Observer Design Pattern

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.

Terms:

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.

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.

GitHub: github.com/tanu02/Design-Patterns/tree/master/Observer/src

Thanks for reading. I hope it helps.

要查看或添加评论,请登录

Tanu Batra的更多文章

  • Amazon Interview Experience

    Amazon Interview Experience

    “Experience is what you get when you didn't get what you wanted. And experience is often the most valuable thing you…

    9 条评论
  • Template Design Pattern

    Template Design Pattern

    Template Design Pattern is one of the Behavioral Design Patterns. Here the structure is constant just the behavior…

    8 条评论
  • Singleton Design Pattern

    Singleton Design Pattern

    This Design Pattern is one of the Creational Design Patterns. The Singleton pattern ensures that the class has only one…

  • Bridge Design Pattern

    Bridge Design Pattern

    (DP: Design Pattern) This is one of the Structural Design Pattern. It "decouples abstraction from its implementation".

    1 条评论
  • Composite Design Pattern

    Composite Design Pattern

    DP: Design Pattern This DP is a Structural Design Pattern. Let’s say a company is allotted a project.

    3 条评论
  • Why are you Progressing Slowly?

    Why are you Progressing Slowly?

    We have limited brain energy every day. There are many sources of brain energy drainage.

  • Why first job matters.

    Why first job matters.

    First job – no joke. You come to know the reality where you stand and what this world expects out of you.

    1 条评论
  • Is MBA worth the money ?

    Is MBA worth the money ?

    Yesterday I attended the QS fair in Mumbai; I just wanted to know what it takes to pursue an MBA. Following are the…

    2 条评论

社区洞察

其他会员也浏览了