先發代碼,有空來寫內容。 observer1 observer2 ...
先發代碼,有空來寫內容。
observer1
1 import java.util.Observer; 2 import java.util.Observable; 3 4 //學生類(Student)繼承Observable後變得可以被觀察 5 class Student extends Observable{ 6 //屬性 7 private String name; 8 private boolean isSleep = false; 9 private boolean isLate = false; 10 //構造函數 11 public Student(){} 12 public Student(String name){ 13 this.name = name; 14 } 15 //睡覺行為 16 public void sleep(){ 17 System.out.println(name + " is sleeping"); 18 isSleep = true; 19 setChanged(); 20 notifyObservers(); 21 22 } 23 public String toString(){ 24 return name; 25 } 26 } 27 //教師類(Teacher)實現Observer介面成為觀察者 28 class Teacher implements Observer{ 29 @Override 30 public void update(Observable o,Object arg){ 31 System.out.println("teacher: "+o+",wakeup!wakeup!"); 32 } 33 } 34 //班主任類(ClassHead)實現Observer介面成為觀察者 35 class ClassHead implements Observer{ 36 @Override 37 public void update(Observable o,Object arg){ 38 System.out.println("classHead: "+o+",cleanTheRoomAfterSchool"); 39 } 40 } 41 public class ObserverDemo{ 42 public static void main(String[] arguments){ 43 Student tom = new Student("Tom"); 44 tom.addObserver(new ClassHead()); 45 tom.addObserver(new Teacher()); 46 47 System.out.println("有 "+tom.countObservers()+" 個觀察者在觀察Tom"); 48 tom.sleep(); 49 50 } 51 }
observer2
import java.util.Map; import java.util.HashMap; import java.util.Observer; import java.util.Observable; //學生類(Student)繼承Observable後變得可以被觀察 class Student extends Observable{ //屬性 private String name; private boolean isSleep = false; private boolean isLate = false; //構造函數 public Student(){} public Student(String name){ this.name = name; } //睡覺行為 public void sleep(){ System.out.println(name + " is sleeping"); isSleep = true; setChanged(); notifyObservers("sleep"); } //遲到行為 public void late(){ System.out.println(name + " was lated"); isLate = true; setChanged(); notifyObservers("late"); } public String toString(){ return name; } } //教師類(Teacher)實現Observer介面成為觀察者 class Teacher implements Observer{ //參數arg作為key,該參數觸發的回應作為value存儲在Map中 private Map<String,String> info = new HashMap<String,String>(); public Teacher(){ info.put("sleep",",wakeup!wakeup!"); info.put("late",",come in."); } //添加鍵值對 public boolean addSay(String k,String v){ return info.put(k,v)==null?false:true; } @Override public void update(Observable o,Object arg){ String say = info.get(arg); if(say == null){ return; } System.out.println("teacher: "+o+say); } } //班主任類(ClassHead)實現Observer介面成為觀察者 class ClassHead implements Observer{ private Map<String,String> info = new HashMap<String,String>(); public ClassHead(){ info.put("sleep",",goOutWashBaFace"); info.put("late",",cleanTheRoom!"); } public boolean addSay(String k,String v){ return info.put(k,v)==null?false:true; } @Override public void update(Observable o,Object arg){ String say = info.get(arg); if(say == null){ return; } System.out.println("classHead: "+o+say); } } public class ObserverDemo{ public static void main(String[] arguments){ Student tom = new Student("Tom"); tom.addObserver(new ClassHead()); tom.addObserver(new Teacher()); System.out.println("有 "+tom.countObservers()+" 個觀察者在觀察Tom"); tom.sleep(); tom.late(); } }