Saturday, 10 October 2015

Observer pattern or Publish-subscribe pattern

Motivation

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) {
            observersList.add(observer);
      }

      public void unRegisterObserver(Observer observer) {
            observersList.remove(observer);
      }

      public void notifyObserver() {

            if(stateChange) {
                  for (Observer observer : observersList) {
                        observer.update();
                  }
            }
      }

      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;
            notifyObserver();
      }
}

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) {
            this.blog = blog;
            article = "No New Article!";
      }

      @Override
      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");
           
            blog.registerObserver(user1);
            blog.registerObserver(user2);
           
            user1.setSubject(blog);
            user2.setSubject(blog);
     
            System.out.println(user1.getArticle());        
            blog.postNewArticle();
            System.out.println(user1.getArticle());
      }
}
Output:
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...