Question: 2 . ( 1 0 pts ) The following is a typical example about head hunter. There are two roles in this example - HeadHunter

2.(10 pts) The following is a typical example about head hunter. There are two roles in this example -HeadHunter and JobSeeker. Job seekers subscribe to a head hunter, and head hunter notifies job seekers when there is a new job opportunity. Draw the UML diagram (class diagram) for this design pattern. Explain the importance of this design pattern.
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyAllObservers();
}
public interface Observer {
public void update(Subject s);
}
import java.util.ArrayList;
public 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();
}
}
public class JobSeeker implements Observer {
private String name;
public JobSeeker(String name){
this.name = name;
}
@Override
public void update(Subject s){
System.out.println(this.name +" got notified!");
//print job list
System.out.println(s);
}
}
public class Main {
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"));
hh.addJob("Google Job");
hh.addJob("Yahoo Job");
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!