Java design pattern-Observer Pattern

Source: Internet
Author: User

The observer mode defines a one-to-many relationship between objects. When an object changes, all observers can be notified.

Headhunting and job seekers are the most typical observer model. Job seekers register at the headhunting office first. When there is a new job opportunity, the headhunting will notify the job seeker.

The following is a class diagram of headhunting and job seekers:

Subject interface:

interface Subject{public void registerObserver(Observer o);public void removeObserver(Observer o);public void notifyAllObservers();}

Observer interface:

interface Observer{public void update(Subject s);}

The Hunter class implements the Subject interface:

class HeadHunter implements Subject{    //define a list of users, such as Mike, Bill, etc.    private ArrayList
 
   userList;    private ArrayList
  
    jobs;    public HeadHunter(){        userList = new ArrayList
   
    ();        jobs = new ArrayList
    
     ();    }    @Override    public void registerObserver(Observer o) {        userList.add(o);    }    @Override    public void removeObserver(Observer o) {    }    @Override    public void notifyAllObservers() {        for(Observer o: userList){            o.update(this);        }    }    public void addJob(String job) {        this.jobs.add(job);        notifyAllObservers();    }    public ArrayList
     
       getJobs() {        return jobs;    }    public String toString(){        return jobs.toString();    }}
     
    
   
  
 

JobSeeker is an observer:

class JobSeeker implements Observer{private String name;public JobSeeker(String name){this.name = name;}@Overridepublic void update(Subject s) {System.out.println(this.name+"  get notified");System.out.println(s);} }
Main Function
Public static void main (String [] args) {HeadHunter hh = new HeadHunter (); hh. registerObserver (new JobSeeker ("Mike"); hh. registerObserver (new JobSeeker ("Chris"); hh. registerObserver (new JobSeeker ("Jeff"); // each time a job is added, all job seekers are notified. Hh. addJob ("Google Job"); hh. addJob ("Yahoo Job ");}




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.