Tuesday, 9 January 2018

Design Patterns | Observer pattern or Publish-subscribe pattern

The observer pattern is a software design pattern in which an object, called 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.
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

It is mainly used to implement distributed event handling systems, in "event-driven" software.


In the 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.


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 notified to all subscribers by SMS or mail alerts.

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

import java.util.ArrayList;
import java.util.List;

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

public class YouTubeChannel implements Subject {

    List<Observer> observersList;
    private boolean stateChange;

    public YouTubeChannel() {
        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 String getUpdate() {
        String 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 youtubeChan;

    public void setSubject(Subject youtubeChan) {
        this.youtubeChan = youtubeChan;
        article = "No New Article!";
    }

    @Override
    public void update() {
        System.out.println("State change reported by Subject " + userName);
        article = youtubeChan.getUpdate();
    }

    public String getArticle() {
        return article;
    }
}

public class ObserverPatternTest {
    public static void main(String args[]) {
        YouTubeChannel blog = new YouTubeChannel();
        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 an 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...