The observer mode consists of the observer and topic.
The core idea of observer mode: When the subject changes, it notifies all observers.
Observer mode: The Observer mode is implemented in two push modes: Push and pull. The topic holds the observer and notifies the observer when there is a change. the difference lies in the transmission of messages:
Push mode: the message is sent to the observer by the topic. The message contains all the useful values in the topic;
Request mode: The topic transmits its reference value to the observer, and the observer determines which values need to be obtained.
Purpose of observer mode: a solution for message broadcasting.
Disadvantages of observer mode: 1. When the number of observers is large, traversing the observer will reduce the performance; 2. Avoiding closed loop as much as possible will lead to false system death in traversal.
Simple Example
Simulate the News Push, including the main body: Server, client. When the server receives information, it pushes the message to various clients.
Push mode
Package observerpattern. server; import observerpattern. client. observer;/*** topic interface */public interface observable {/*** add an observer to the topic ** @ Param ob * @ return */string add (Observer ob ); /*** remove the observer from the topic ** @ Param ob * @ return */string remove (Observer ob ); /*** send message ** @ Param MSG * @ return */string sendmessage (Message MSG );}
package observerpattern.server;import java.util.ArrayList;import java.util.List;import observerpattern.client.Observer;public class Server implements Observable{private List<Observer> ls = new ArrayList<Observer>();@Overridepublic String add(Observer ob) {if(ob != null && !ls.contains(ob))ls.add(ob);return Constants.SUCCESS;}@Overridepublic String remove(Observer ob) {if(ob!=null && ls.contains(ob))ls.remove(ob);return Constants.SUCCESS;}@Overridepublic String sendMessage(Message msg) {for (Observer ob : ls) {ob.showMessage(msg);}return Constants.SUCCESS;}}
package observerpattern.server;public class Message {private String title;private String contents;private String time;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContents() {return contents;}public void setContents(String contents) {this.contents = contents;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}}
Package observerpattern. server; public class constants {private constants () {} public static final string success = "success"; // success}
Package observerpattern. client; import observerpattern. server. message;/*** observer interface */public interface observer {/*** Display message ** @ Param MSG * @ return */void showmessage (Message MSG );}
package observerpattern.client;import observerpattern.server.Message;public class Client implements Observer{private String name;public Client(String name){this.name = name;}@Overridepublic void showMessage(Message msg) {System.out.println(name+":Title->"+msg.getTitle()+" Time->"+msg.getTime()+" Contents->"+msg.getContents());}}
Request Mode
Simple modification based on Push mode
Package observerpattern. server; import observerpattern. client. observer;/*** topic interface */public interface observable {/*** add an observer to the topic ** @ Param ob * @ return */string add (Observer ob ); /*** remove the observer from the topic ** @ Param ob * @ return */string remove (Observer ob ); /*** send message ** @ Param MSG * @ return */string sendmessage (Message MSG ); /*** pass the information to the observer ** @ Param MSG */void policyobservers ();}
package observerpattern.server;import java.util.ArrayList;import java.util.List;import observerpattern.client.Observer;public class Server implements Observable{private List<Observer> ls = new ArrayList<Observer>();@Overridepublic String add(Observer ob) {if(ob != null && !ls.contains(ob))ls.add(ob);return Constants.SUCCESS;}@Overridepublic String remove(Observer ob) {if(ob!=null && ls.contains(ob))ls.remove(ob);return Constants.SUCCESS;}@Overridepublic String sendMessage(Message msg) {this.msg = msg;notifyObservers();return Constants.SUCCESS;}private Message msg = null;@Overridepublic void notifyObservers() {for (Observer ob : ls) {ob.saveMessage(msg);}}}
package observerpattern.server;public class Message {private String title;private String contents;private String time;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContents() {return contents;}public void setContents(String contents) {this.contents = contents;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}}
Package observerpattern. server; public class constants {private constants () {} public static final string success = "success"; // success}
Package observerpattern. client; import observerpattern. server. message;/*** observer interface */public interface observer {/*** Display message ** @ Param MSG * @ return */void showmessage (); void savemessage (Message MSG );}
package observerpattern.client;import observerpattern.server.Message;public class Client implements Observer{private String name;private Message msg = null;public Client(String name){this.name = name;}@Overridepublic void showMessage() {System.out.println(name+":Title->"+msg.getTitle()+" Time->"+msg.getTime()+" Contents->"+msg.getContents());}@Overridepublic void saveMessage(Message msg) {this.msg = msg;}}
Package observerpattern. test; import observerpattern. client. client; import observerpattern. client. observer; import observerpattern. server. message; import observerpattern. server. observable; import observerpattern. server. server; public class test {public static void main (string [] ARGs) {observable Server = new server (); Observer ob1 = new client ("phone "); observer ob2 = new client ("pad"); Observer ob3 = new client ("computer"); server. add (ob1); server. add (ob2); server. add (ob3); message MSG = new message (); MSG. settitle ("news title"); MSG. settime ("2014/10/31"); MSG. setcontents ("All are haha"); server. sendmessage (MSG); ob1.showmessage ();}}
Running result
Phone: title-> News Title time-> 2014/10/31 contents-> Yes
Observer mode Summary