Java Observer Pattern and observerpattern
Observer Mode
Define one-to-multiple dependencies between objects. In this way, when an object changes its state, all objects dependent on it will receive notifications and automatically update
Package gx. subject; import gx. observer. observer;/*** topic interface ** @ author always **/public interface Subject {void registerObserver (Observer observer); void removeObserver (Observer observer); void policyobserver ();}
Package gx. observer;/***** interface for displaying data after the notification is received ** @ author always **/public interface Display {void display ();}
Package gx. observer;/***** Observer interface ** @ author always **/public interface observer {/*** pull: the data that the Observer wants: "pull" */void update ();/*** push: push all messages to the observer ** @ param tiltle * @ param name * @ param summary */void update (String tiltle, String name, String summary );}
Package gx. observer. impl; import gx. observer. display; import gx. observer. observer; import gx. subject. subject; import gx. subject. impl. currentSubject; public class StudentObserver implements Observer, Display {private Subject subject; private String title; private String name; private String summary; public StudentObserver () {} public StudentObserver (Subject subject) {this. subject = subject; subject. registerObserver (this);} public void update () {if (subject instanceof CurrentSubject) {CurrentSubject cs = (CurrentSubject) subject; this. name = cs. getName (); this. title = cs. getTitle (); this. display () ;}} public void update (String title, String name, String summary) {this. title = title; this. name = name; this. summary = summary; this. display ();} public void display () {System. out. println ("I am a student. The news I subscribe to today is \ n news title:" + title + ", news Author:" + name + ", news introduction: "+ summary + ";");}}
Package gx. observer. impl; import gx. observer. display; import gx. observer. observer; import gx. subject. subject; import gx. subject. impl. currentSubject; public class TeacherObserver implements Observer, Display {private Subject subject; private String title; private String name; private String summary; public TeacherObserver () {} public TeacherObserver (Subject subject) {this. subject = subject; subject. registerObserver (this);} public void update () {if (subject instanceof CurrentSubject) {CurrentSubject cs = (CurrentSubject) subject; this. name = cs. getName (); this. title = cs. getTitle (); this. display () ;}} public void update (String title, String name, String summary) {this. title = title; this. name = name; this. summary = summary; this. display ();} public void display () {System. out. println ("I am a teacher. The news I subscribe to today is \ n news title:" + title + ", news Author:" + name + ", news introduction: "+ summary + ";");}}
Package gx. subject. impl; import gx. observer. observer; import gx. subject. subject; import java. util. arrayList; import java. util. list;/*** example: simulate a student, the teacher subscribes to a newspaper ** this object changes dependent on it. Objects in the observerList will receive a notification * setData --> policyobserver --> observer. update () --> observer. display () * @ author always **/public class CurrentSubject implements Subject {private List <Observer> observerList; private String title; private Stri Ng name; private String summary; public CurrentSubject () {observerList = new ArrayList <Observer> ();} public void registerObserver (Observer observer) {if (observer! = Null) {observerList. add (observer) ;}} public void removeObserver (Observer observer) {int indexOf = observerList. indexOf (observer); if (indexOf! =-1) {observerList. remove (indexOf) ;}} public void policyobserver () {for (Observer observer: observerList) {observer. update (title, name, summary) ;}} public void setMessage (String title, String name, String summary) {this. title = title; this. name = name; this. summary = summary; notifyObserver ();} public String getTitle () {return title;} public String getName () {return name;} public String getSummary () {return summary ;}}
Test class
Package gx. test; import junit. framework. testCase; import gx. observer. observer; import gx. observer. impl. studentObserver; import gx. observer. impl. teacherObserver; import gx. subject. impl. currentSubject; public class TestObserver extends TestCase {public void testObserverPush () {CurrentSubject cs = new CurrentSubject (); Observer observer Observer = new StudentObserver (cs); cs. registerObserver (new StudentObserver (); cs. registerObserver (new TeacherObserver (); cs. setMessage ("design mode", "White", "hello"); // The topic status changes: this. notifyObserver --> observer. update --> observer. display }}