Observer design pattern is useful when you are interested in the state of an object and want to get notified whenever there is any change.
Use observer mode when you want to be notified of any changes in the state of an object.
Observer design pattern is also called as publish-subscribe pattern.
The observer design pattern is also known as the publish subscribe pattern.
According to GoF, observer design pattern intent is;
According to GoF, the intention of observer design pattern is;
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Define one to many dependencies between objects, so that when an object changes state, all its dependencies will be notified and updated automatically.
There are many examples of observer patterns in life,
For example, we usually turn on and off the lights. When we turn on the light switch, the light comes on immediately; When we turned off the light, the light went out immediately. In this process, the lamp responds to the event that we control the switch, which is the simplest one-to-one observer mode.
The light is the Observer
Switch Observable
This behavior of the lamp observation switch is called subscribe, or register
When the switch changes, the lamp also changes, which is called update
When the official account official account official account, all readers who have been concerned about the public address immediately received the article. During this process, all the WeChat clients who were concerned about the official account responded to the public address issue, which is a typical one to many observer mode.
Wechat client is Observer
The official account is observed (Observable, observable).
The behavior of paying attention to the official account is called subscribe, or registration (register).
The official account sends information, and the client receives the information called update.
Another example of observer mode,
For example, the sentry has been observing the wind and grass on the battlefield. If there is an enemy situation on the battlefield, the sentry takes action immediately. In this process:
The sentry calls it the Observer
The battlefield is called Observable
This behavior of sentinels observing the battlefield is called subscribe, or register
When an enemy situation occurs, the sentry takes action, which is called update
Observer interface
public interface A1_Subject { //methods to register and unregister observers public void register(A2_Observer obj); public void unregister(A2_Observer obj); //method to get updates from subject can be viewed by the observer at any time public Object getUpdate(A2_Observer obj); //method to notify observers of change public void notifyObservers(); }
Observed
public class A3_MyTopic implements A1_Subject { private List<A2_Observer> observers; private String message; private boolean changed; private final Object MUTEX= new Object(); public A3_MyTopic(){ this.observers=new ArrayList<>(); } @Override public void register(A2_Observer obj) { if(obj == null) throw new NullPointerException("Null Observer"); synchronized (MUTEX) { if(!observers.contains(obj)) observers.add(obj); } } @Override public void unregister(A2_Observer obj) { synchronized (MUTEX) { observers.remove(obj); } } @Override public Object getUpdate(A2_Observer obj) { return this.message; } //method to post message to the topic public void postMessage(String msg){ System.out.println("Message Posted to Topic:" + msg); this.message = msg; this.changed = true; notifyObservers(); } @Override public void notifyObservers() { List<A2_Observer> observersLocal = null; //synchronization is used to make sure any observer registered after message is received is not notified synchronized (MUTEX) { if (!changed) { return; } observersLocal = new ArrayList<>(this.observers); this.changed=false; } for (A2_Observer obj : observersLocal) { obj.update(); } } }
Observer interface
public interface A2_Observer { //attach with subject to observe public void setSubject(A1_Subject sub); //method to update the observer, used by subject public void update(); }
Observer
public class A4_MyTopicSubscriber implements A2_Observer { private String name; private A1_Subject topic; public A4_MyTopicSubscriber(String nm){ this.name = nm; } @Override public void setSubject(A1_Subject sub) { this.topic = sub; } @Override public void update() { String msg = (String) topic.getUpdate(this); if (msg == null) { System.out.println(name + ":: No new message"); } else { System.out.println(name + ":: Consuming message::" + msg); } } }
use
public static void main(String[] args) { //create subject observed A3_MyTopic topic = new A3_MyTopic(); //create observers A2_Observer obj1 = new A4_MyTopicSubscriber("Obj1"); A2_Observer obj2 = new A4_MyTopicSubscriber("Obj2"); A2_Observer obj3 = new A4_MyTopicSubscriber("Obj3"); //register observers to the subject tells the observers who have observed them in order to inform them not to drop people topic.register(obj1); topic.register(obj2); topic.register(obj3); //attach observer to subject tells the observer who to observe in order to obtain the observed state at any time obj1.setSubject(topic); obj2.setSubject(topic); obj3.setSubject(topic); //check if any update is available the observer can view the information of the observed at any time obj1.update(); //now send message to subject the observer can publish information to the observer at any time topic.postMessage("New Message"); }
Java Message Service (JMS) uses Observer design pattern along with Mediator pattern to allow applications to subscribe and publish data to other applications.
Java Message Service (JMS) uses observer design patterns and mediation patterns to allow applications to subscribe and publish data to other applications.
Model-View-Controller (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 model view controller (MVC) framework also uses the observer pattern, where the model is the host and the view is the observer, which can be registered to be notified of any changes to the model.
java.util.EventListener in Swing
javax.servlet.http.HttpSessionBindingListener
javax.servlet.http.HttpSessionAttributeListener