Java Design pattern-------Observer pattern

Source: Internet
Author: User

* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.