The observer pattern, as the name implies, is the constant attention of an object or thread to an object or thread, and if any changes occur to the object or thread being followed, the observer can be notified in a timely manner and the correct response is made.
Observer patterns are very common in development, such as some management systems, or e-commerce systems, which create an observer thread that pays attention to the threads that need attention and, once an event occurs, notifies the observer to react. For example, a factory production management system, once some important indicators beyond the alert line, the need for timely notification of the observer thread, can be based on different problem conditions to build different observers, different observers can be different ways or channels to notify users, such as sending text messages, sending mail, push mobile phone app messages, etc. Avoid the occurrence of production accidents.
The following is a simple example of the observer pattern, which is simply a reflection of the core idea of the observer pattern, which is relatively simple:
Package Dp.observer;public interface Subject {public void Register (Observer obj); public void unregister (Observer obj); public void Notifyobservers ();}
Package Dp.observer;import Java.util.arraylist;import Java.util.list;public class MyTopic implements Subject {private list<observer> observers;private String messagestring = "Server Notification";p ublic MyTopic () {this.observers = new arraylist& Lt;> ();} @Overridepublic void Register (Observer obj) {if (!observers.contains (obj)) observers.add (obj);} @Overridepublic void unregister (Observer obj) {observers.remove (obj);} @Overridepublic void Notifyobservers () {for (Observer obj:this.observers) {obj.update (messagestring + ", Occurrence time:" + System . Currenttimemillis ());}}
Package Dp.observer;public interface Observer {public void Update (String message);p ublic void Setsubject (Subject sub);}
Package Dp.observer;public class Mytopicsubscriber implements observer { private String name; Private Subject topic; Public Mytopicsubscriber (String nm) { this.name=nm; } @Override public void Update (String message) { System.out.println (name+ "received information from the server:" +message); } @Override public void Setsubject (Subject sub) { this.topic=sub; }}
Package Dp.observer;public class Main {public static void Main (string[] args) { MyTopic topic = new MyTopic ();
observer obj1 = new Mytopicsubscriber ("Observer 1"); Observer obj2 = new Mytopicsubscriber ("Observer 2"); Observer obj3 = new Mytopicsubscriber ("Observer 3"); Topic.register (obj1); Topic.register (OBJ2); Topic.register (OBJ3); Obj1.setsubject (topic); Obj2.setsubject (topic); Obj3.setsubject (topic); Topic.notifyobservers (); } }
Operation Result:
Observer 1 received information from server: Server notification, time of occurrence: 1439225528247Observer 2 received information from server: Server notification, time: 1439225528247Observer 3 Receive information from server: Server notification, time: 1439225528247
Observer Design Pattern (Observer mode)