Saturday, 10 October 2015

Observer pattern or Publish-subscribe pattern


Let's assume we have a channel on YouTube which is subscribed by many users. Now any new video uploaded on that channel should be notify to all subscribers by SMS or mail alerts.

Observer pattern: we need to separate the subject (YouTube channel) from its observers (subscribers) in such a way that adding new observer (subscription for new user) will be transparent for the server.

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

In observer pattern, the objects that watch on the state of another object are called Observers and the object that is being watched is called Subject. 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.

It is mainly used to implement distributed event handling systems.

public interface Observer {

      public void update();

      public void setSubject(Subject subject);

public interface Subject {

      public void registerObserver(Observer observer);

      public void notifyObserver();

      public void unRegisterObserver(Observer observer);

      public Object getUpdate();

import java.util.ArrayList;
import java.util.List;
public class Blog implements Subject {

      List<Observer> observersList;
      private boolean stateChange;

      public Blog() {
            this.observersList = new ArrayList<Observer>();
            stateChange = false;

      public void registerObserver(Observer observer) {

      public void unRegisterObserver(Observer observer) {

      public void notifyObserver() {

            if(stateChange) {
                  for (Observer observer : observersList) {

      public Object getUpdate() {
            Object changedState = null;
            // should have logic to send the
            // state change to querying observer
            if (stateChange) {
                  changedState = "Observer Design Pattern";
            return changedState;

      public void postNewArticle() {
            stateChange = true;

public class User implements Observer {
      public User(String userName) {
            this.userName = userName;

      private String userName;
      private String article;
      private Subject blog;

      public void setSubject(Subject blog) {
   = blog;
            article = "No New Article!";

      public void update() {
            System.out.println("State change reported by Subject "+ userName);
            article = (String) blog.getUpdate();

      public String getArticle() {
            return article;

public class ObserverDesignPattern {
      public static void main(String args[]) {
            Blog blog = new Blog();
            User user1 = new User("user 1");
            User user2 = new User("user 2");
No New Article!
State change reported by Subject to user 1
State change reported by Subject to user 2
Observer Design Pattern

Usage of Observer design pattern

Java Message Service (JMS) uses Observer pattern along with Mediator pattern to allow applications to subscribe and publish data to other applications.

MVC frameworks also use Observer pattern where Model is the Subject and Views are observers that can register to get notified of any change to the model.

The observer pattern is implemented in numerous programming libraries and systems, including almost all GUI toolkits.

Java provides inbuilt platform for implementing Observer pattern through java.util.Observable class and java.util.Observer interface.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...