* In simple terms, the viewer mode = Publisher + Subscriber. Here is a typical example of a headhunter. In the following diagram, there are two characters:
* Headhunters and people looking for work. The person who is looking for a job subscribes to a headhunter and tells that he wants to get a job, and when there is a new job opportunity,
* The Headhunter will notify the person who has subscribed to the message.
Headhunting Interface:
Package com.pattern.observer;/** * @author Qixuan.chen * Created: September 2, 2014 */public interface Subject {public void Regis Terobserver (Observer Observer); public void Removeobserver (Observer Observer); public void Notifyallobservers ();}
Subscriber Interface:
Package com.pattern.observer;/** * @author Qixuan.chen * Created: September 2, 2014 */PUBLIC interface Observer {//Viewer update public void up Date (Subject Subject);}
Headhunting Implementation class:
Package Com.pattern.observer;import java.util.arraylist;import java.util.list;/** * @author Qixuan.chen * Created: September 2, 2014 * headhunting classes */public class Headcounter implements Subject {private list<observer> observerlist; Private list<string> joblist; Public Headcounter () {observerlist = new arraylist<observer> (); Joblist = new arraylist<string> (); }//Implement interface method//Get registered person @Override public void Registerobserver (final Observer Observer) {if (! Observer List.contains (Observer)) {OBSERVERLIST.ADD (Observer); }}//delete a registered person @Override public void Removeobserver (final Observer Observer) {if (Observerlist.contain S (Observer)) {OBSERVERLIST.REMOVE (Observer); }}//notification @Override public void Notifyallobservers () {for (Observer observer:observerlist) { Observer.update (this); }}//Add new work public void AddJob (final String job) {This.jobList.add (Job); Notifyallobservers (); }//Get work Public list<string> Getjobs () {return joblist; } public String toString () {return joblist.tostring (); }}
Subscriber Implementation classes:
Package com.pattern.observer;/** * @author Qixuan.chen * Created: September 2, 2014 * Subscriber classes */public class Jobseeker implements Observe R{private string name;//Subscriber name public jobseeker (string name) { this.name = name; } Method of implementing the interface public void Update (Subject Subject) { System.out.println ("----to-------" +this.name + "------notifications"); System.out.println (subject); }}
Test class:
Package com.pattern.observer;/** * @author Qixuan.chen * Created: September 2, 2014 * In simple terms, the viewer mode = Publisher + Subscriber. Here is a typical example of a headhunter. In the following chart there are two characters: * Headhunters and people looking for work. The person who is looking for a job subscribes to a headhunter, tells him that he wants a job, and when there is a new job opportunity, the Headhunter will notify the person who has subscribed to him. * two interfaces, two implementations, one Test */public class Testdemo {public static void main (string[] args) {System.out.println ("----------Program Entry- -----------"); Headcounter headcounter = new Headcounter (); Registered subscriber Headcounter.registerobserver (new Jobseeker ("Mike")); Headcounter.registerobserver (New Jobseeker ("Chris")); Headcounter.registerobserver (New Jobseeker ("Jeff")); Notify all Subscribers of new job opportunities Headcounter.addjob ("Google job"); Headcounter.addjob ("Yahoo Job"); }}
Java Design pattern-------Observer pattern