Software

Observer Pattern

Visualization example: let’s say you want to know about the new articles of this blog. You can make software which will check the updates every 1 minute or every 1 hour or even a day. But maybe you need to know immediately, then the observer pattern can help, where the blog will notify you every time a new post is added. You would subscribe to the blog. Every time when new post is published, the blog will notify each subscriber, including you. Hence you don’t need to pull for this information at some interval.

Subject – blog, will keep the list of Observers (subscribers to the blog).

We’ll have a Subject superclass, that defines three methods:

  • Allow a new observer to subscribe.
  • Allow a current observer to unsubscribe.
  • Notify all observers about a new blog post.
Continue reading “Observer Pattern”