1 package com.shejimoshi.behavioral.Observer; 2 3 4 /** 5 * 功能:学生观察者 6 * 时间:2016年3月7日上午10:28:00 7 * 作者:cutter_point 8 */ 9 public interface Student10 {11 //根据相应的事件作出反应12 public abstract void reaction();13 }
1 package com.shejimoshi.behavioral.Observer; 2 3 4 /** 5 * 功能:学生类具体观察者 6 * 时间:2016年3月7日上午10:30:57 7 * 作者:cutter_point 8 */ 9 public class SeniorHighSchoolStudents implements Student10 {11 //老师12 private Teacher teacher;13 private String name; //名字14 15 public SeniorHighSchoolStudents()16 {17 }18 19 public SeniorHighSchoolStudents(Teacher t, String n)20 {21 this.teacher = t;22 this.name = n;23 }24 25 /**26 * 学生做出相应的反应27 */28 @Override29 public void reaction()30 {31 String action = teacher.getAction();32 System.out.println("老师说:" + action + "\t" + name + "回到教室,准备学习");33 }34 35 }
1 package com.shejimoshi.behavioral.Observer; 2 3 4 /** 5 * 功能:老师类,作为通知者 6 * 时间:2016年3月7日上午10:31:16 7 * 作者:cutter_point 8 */ 9 public interface Teacher10 {11 /**12 * 添加学生13 * @param student14 */15 public void addStrudnet(Student student);16 17 /**18 * 开除一个同学19 * @param student20 */21 public void removeStudent(Student student);22 23 /**24 * 开始上课25 */26 public void classBegin();27 28 /**29 * 取得通知事件30 * @return31 */32 public String getAction();33 }
1 package com.shejimoshi.behavioral.Observer; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 7 /** 8 * 功能:曹老师 9 * 时间:2016年3月7日上午10:36:3010 * 作者:cutter_point11 */12 public class TeacherChao implements Teacher13 {14 private List<Student> ls = new ArrayList<Student>();15 private String action; //事件16 17 @Override18 public void addStrudnet(Student student)19 {20 ls.add(student);21 }22 23 @Override24 public void removeStudent(Student student)25 {26 ls.remove(student);27 }28 29 @Override30 public void classBegin()31 {32 33 for(Student s : ls)34 {35 //通知上课开始36 s.reaction();37 }//for38 }39 40 public String getAction()41 {42 return action;43 }44 45 public void setAction(String action)46 {47 this.action = action;48 }49 50 }
测试代码:
1 package com.shejimoshi.behavioral.Observer; 2 3 4 /** 5 * 功能:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变的时候,所有依赖于它的对对象都得到通知并被自动更新 6 * 适用:当一个抽象模式有两个方面的时候,其中一个方面依赖另外一个方面。将这两种封装在独立的对象中以使它们可以独立地改变和复用 7 * 当对一个对象的改变需要同时改变其他对象,而不知道具体有多少对象有待改变 8 * 当一个对象必须通知其他对象,而它又不能假定其它对象是谁。换言之,你不希望这些对象时紧密耦合的 9 * 时间:2016年3月7日上午8:32:1810 * 作者:cutter_point11 */12 public class Test13 {14 public static void main(String[] args)15 {16 TeacherChao tc = new TeacherChao();17 //学生18 SeniorHighSchoolStudents xiaoming = new SeniorHighSchoolStudents(tc, "小明");19 SeniorHighSchoolStudents xiaocheng = new SeniorHighSchoolStudents(tc, "小城");20 SeniorHighSchoolStudents xiaohong = new SeniorHighSchoolStudents(tc, "小红");21 SeniorHighSchoolStudents xiaozhi = new SeniorHighSchoolStudents(tc, "小智");22 SeniorHighSchoolStudents xiaohua = new SeniorHighSchoolStudents(tc, "笑话");23 24 tc.addStrudnet(xiaoming); tc.addStrudnet(xiaocheng); tc.addStrudnet(xiaohong);25 tc.addStrudnet(xiaozhi); tc.addStrudnet(xiaohua);26 27 tc.setAction("上课");28 tc.classBegin(); //老师上课啦29 30 }31 }
测试结果:
老师说:上课 小明回到教室,准备学习老师说:上课 小城回到教室,准备学习老师说:上课 小红回到教室,准备学习老师说:上课 小智回到教室,准备学习老师说:上课 笑话回到教室,准备学习
原标题:【设计模式】19、观察者模式
关键词:设计模式