设计模式----观察者模式(observer)
这里采用两种方式实现观察者模式:第一种方式为完全自己写,第二种方式为实现JDK 的Observable和Observer接口
package com.designPattern.abserver;public interface Observer {public void update(String context);}
package com.designPattern.abserver;public class LiSi implements Observer{@Overridepublic void update(String context) { System.out.println("李斯:观察到韩非子的活动了,开始向老板汇报!"); this.report(context); System.out.println("李斯:汇报完毕!");}private void report(String reportContext){System.out.println("李斯:报告秦始皇,李斯有活动:"+reportContext);}}
package com.designPattern.abserver;public class LiuSi implements Observer{@Overridepublic void update(String context) { System.out.println("刘斯:观察到韩非子活动!"); this.happy(context); System.out.println("刘斯:真被乐死了!");}private void happy(String context){System.out.println("刘斯:因为"+context+",--所以我很快乐!");}}
package com.designPattern.abserver;public class WangSi implements Observer{@Overridepublic void update(String context) { System.out.println("王斯:观察到韩非子活动!"); this.cry(context); System.out.println("王斯:真真的哭死了!");}private void cry(String context){System.out.println("王斯:因为"+context+"---所以悲伤!");}}
package com.designPattern.abserver;/** * * @author yangjianzhou * 所有被观察者 */public interface Observable {public void addObserver(Observer observer);public void deleteObserver(Observer observer);public void notifyObservers(String context);}
package com.designPattern.abserver;import java.util.ArrayList;public class HanFeiZi implements Observable{private ArrayList<Observer> observerList = new ArrayList<Observer>();@Overridepublic void addObserver(Observer observer) {this.observerList.add(observer);}@Overridepublic void deleteObserver(Observer observer) {this.observerList.remove(observer);}@Overridepublic void notifyObservers(String context) {for(Observer observer:observerList){observer.update(context);}}public void haveBreakfast(){System.out.println("韩非子:开始吃饭了。。。");this.notifyObservers("韩非子正在吃饭");}public void haveFun(){System.out.println("韩非子正在娱乐!");this.notifyObservers("韩非子正在娱乐");}}
package com.designPattern.abserver;public class Client {public static void main(String[] args) {Observer liSi = new LiSi();Observer wangSi = new WangSi();Observer liuSi = new LiuSi();HanFeiZi hanFeiZi = new HanFeiZi();hanFeiZi.addObserver(liSi);hanFeiZi.addObserver(wangSi);hanFeiZi.addObserver(liuSi);hanFeiZi.haveBreakfast();}}
韩非子:开始吃饭了。。。李斯:观察到韩非子的活动了,开始向老板汇报!李斯:报告秦始皇,李斯有活动:韩非子正在吃饭李斯:汇报完毕!王斯:观察到韩非子活动!王斯:因为韩非子正在吃饭---所以悲伤!王斯:真真的哭死了!刘斯:观察到韩非子活动!刘斯:因为韩非子正在吃饭,--所以我很快乐!刘斯:真被乐死了!
package com.designPattern.abserverAPI;import java.util.Observable;import java.util.Observer;public class LiSi implements Observer{@Overridepublic void update(Observable observable, Object obj) { System.out.println("李斯:观察到韩非子的活动了,开始向老板汇报!"); this.report(obj.toString()); System.out.println("李斯:汇报完毕!");}private void report(String reportContext){System.out.println("李斯:报告秦始皇,李斯有活动:"+reportContext);}}
package com.designPattern.abserverAPI;import java.util.Observable;import java.util.Observer;public class LiuSi implements Observer{@Overridepublic void update(Observable observable, Object obj) { System.out.println("刘斯:观察到韩非子活动!"); this.happy(obj.toString()); System.out.println("刘斯:真被乐死了!");}private void happy(String context){System.out.println("刘斯:因为"+context+",--所以我很快乐!");}}
package com.designPattern.abserverAPI;import java.util.Observable;import java.util.Observer;public class WangSi implements Observer{@Overridepublic void update(Observable observable, Object obj) { System.out.println("王斯:观察到韩非子活动!"); this.cry(obj.toString()); System.out.println("王斯:真真的哭死了!");}private void cry(String context){System.out.println("王斯:因为"+context+"---所以悲伤!");}}
package com.designPattern.abserverAPI;import java.util.Observable;/** * * @author yangjianzhou * 使用JDK自带的Observable类 */public class HanFeiZi extends Observable{public void haveBreakfast(){System.out.println("韩非子:开始吃饭了。。。");super.setChanged();super.notifyObservers("韩非子正在吃饭");}public void haveFun(){System.out.println("韩非子正在娱乐!");super.setChanged();super.notifyObservers("韩非子正在娱乐");}}
package com.designPattern.abserverAPI;import com.designPattern.abserver.HanFeiZi;import com.designPattern.abserver.LiSi;import com.designPattern.abserver.LiuSi;import com.designPattern.abserver.Observer;import com.designPattern.abserver.WangSi;public class Client {public static void main(String[] args) {Observer liSi = new LiSi();Observer wangSi = new WangSi();Observer liuSi = new LiuSi();HanFeiZi hanFeiZi = new HanFeiZi();hanFeiZi.addObserver(liSi);hanFeiZi.addObserver(wangSi);hanFeiZi.addObserver(liuSi);hanFeiZi.haveBreakfast();}}
韩非子:开始吃饭了。。。李斯:观察到韩非子的活动了,开始向老板汇报!李斯:报告秦始皇,李斯有活动:韩非子正在吃饭李斯:汇报完毕!王斯:观察到韩非子活动!王斯:因为韩非子正在吃饭---所以悲伤!王斯:真真的哭死了!刘斯:观察到韩非子活动!刘斯:因为韩非子正在吃饭,--所以我很快乐!刘斯:真被乐死了!