一、JDK方法實現案例需求:java
去餐廳吃飯有時候須要排隊,進行排隊叫號。假如全部等待的人都是觀察者,叫號系統就是一個被監聽的主題對象。
當叫號系統發出叫號通知後,全部等待的人,都會收到通知,而後檢查本身的號碼是否匹配。匹配的話,就開始就餐,
不匹配的,繼續等待
package com.gupao.jay.pattern.observer.jdk; /** * @Author JAY * @Date 2019/6/2 8:41 * @Description 排隊的號碼 **/ public class NumberModel { private int number; private String deskType; public NumberModel(int number, String deskType) { this.number = number; this.deskType = deskType; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getDeskType() { return deskType; } public void setDeskType(String deskType) { this.deskType = deskType; } @Override public String toString() { return "NumberModel{" + "number=" + number + ", deskType='" + deskType + '\'' + '}'; } }
1 package com.gupao.jay.pattern.observer.jdk; 2 3 import java.util.Observable; 4 import java.util.Observer; 5 6 /** 7 * @Author JAY 8 * @Date 2019/6/2 8:40 9 * @Description 等待的顧客 10 **/ 11 public class Consumer implements Observer { 12 13 public Consumer(int number) { 14 this.number = number; 15 } 16 17 private int number; 18 19 public int getNumber() { 20 return number; 21 } 22 23 public void setNumber(int number) { 24 this.number = number; 25 } 26 27 @Override 28 public void update(Observable o, Object arg) { 29 NumberModel numberModel = (NumberModel) arg; 30 if (numberModel.getNumber() != this.number){ 31 System.out.println("號碼不匹配,繼續等待。。。。"); 32 return; 33 } 34 System.out.println("排到咱們了,前往" + numberModel.getDeskType() + "用餐"); 35 } 36 }
package com.gupao.jay.pattern.observer.jdk; import java.util.Observable; import java.util.Observer; import java.util.concurrent.locks.Lock; /** * @Author JAY * @Date 2019/6/2 8:42 * @Description 叫號系統 **/ public class CallNumberSystem extends Observable { private CallNumberSystem(){} private static CallNumberSystem callNumberSystem = null; public static CallNumberSystem getInstance(){ if (callNumberSystem == null){ synchronized (CallNumberSystem.class){ if (callNumberSystem == null){ callNumberSystem = new CallNumberSystem(); } } } return callNumberSystem; } public void notifyNumber(NumberModel numberModel){ System.out.println("系統播報:請" + numberModel.getNumber() + "號顧客到" + numberModel.getDeskType() + "就餐!"); setChanged(); notifyObservers(numberModel); } }
package com.gupao.jay.pattern.observer.jdk; /** * @Author JAY * @Date 2019/6/2 8:55 * @Description TODO **/ public class CallNumberSystemTest { public static void main(String[] args) { Consumer consumerA = new Consumer(10001); Consumer consumerB = new Consumer(10002); Consumer consumerC = new Consumer(10003); CallNumberSystem instance = CallNumberSystem.getInstance(); instance.addObserver(consumerA); instance.addObserver(consumerB); instance.addObserver(consumerC); instance.notifyNumber(new NumberModel(10002,"小桌A11號")); } }
二、guavaAPI實現案例需求:模擬在社區中,學生向老師提問。spring
1 package com.gupao.jay.pattern.observer.guava; 2 3 /** 4 * @Author JAY 5 * @Date 2019/6/2 9:11 6 * @Description 問題 7 **/ 8 public class Question { 9 10 private String userName; 11 private String content; 12 13 public String getUserName() { 14 return userName; 15 } 16 17 public void setUserName(String userName) { 18 this.userName = userName; 19 } 20 21 public String getContent() { 22 return content; 23 } 24 25 public void setContent(String content) { 26 this.content = content; 27 } 28 }
package com.gupao.jay.pattern.observer.guava; import com.google.common.eventbus.Subscribe; /** * @Author JAY * @Date 2019/6/2 9:05 * @Description TODO **/ public class Teacher { private String name; public Teacher(String name) { this.name = name; } @Subscribe public void subscribe(Question question){ //業務邏輯 System.out.println(this.name + "老師,學生" + question.getUserName() + "向你提了一個問題,問題的內容是:" + question.getContent()); } }
1 package com.gupao.jay.pattern.observer.guava; 2 3 import com.google.common.eventbus.EventBus; 4 5 /** 6 * @Author JAY 7 * @Date 2019/6/2 9:11 8 * @Description TODO 9 **/ 10 public class GuavaTest { 11 public static void main(String[] args) { 12 Question question = new Question(); 13 question.setUserName("JayNing"); 14 question.setContent("如何閱讀spring源碼?"); 15 16 Teacher tom = new Teacher("Tom"); 17 Teacher mic = new Teacher("Mic"); 18 19 EventBus eventBus = new EventBus(); 20 eventBus.register(tom); 21 eventBus.register(mic); 22 eventBus.post(question); 23 } 24 }