观察者模式

李斯和韩非子都是荀子的学生,李斯是师兄,韩非子是师弟,若干年后,李斯成为秦国的上尉,致力于统一全国,于是安插了间谍到各个国家的重要人物的身边进行监视,韩非子身边也有很多间谍,韩非子早饭吃的什么,晚上在做什么娱乐,李斯都了如指掌,我们先通过程序把这个过程展现一下,看看李斯是怎么监控韩非子的,先看类图:

观察者模式_第1张图片

public class HanFeiZi implements IHanFeiZi{
    private boolean isHavingBreakfast = false;
    private boolean isHavingFun = false;
    @Override
    public void haveBreakfast() {
        System.out.println("韩非子:开始吃饭了...");
        this.isHavingBreakfast = true;
    }
    public void haveFun(){
        System.out.println("韩非子:开始娱乐了...");
        this.isHavingFun = true;
    }

    public boolean isHavingBreakfast() {
        return isHavingBreakfast;
    }

    public void setHavingBreakfast(boolean ishavingBreakfast) {
        this.isHavingBreakfast = ishavingBreakfast;
    }

    public boolean isHavingFun() {
        return isHavingFun;
    }

    public void setHavingFun(boolean ishavingFun) {
        this.isHavingFun = ishavingFun;
    }
}
public interface IHanFeiZi {
    public void haveBreakfast();
    public void haveFun();
}
public interface ILiSi {
    //一发现别人有动静,自己也要行动起来
    public void update(String context);
}
public class LiSi implements ILiSi{
    @Override
    public void update(String context) {
        System.out.println("李斯:观察到韩非子活动,开始向老板汇报了。。。");
        this.reportToQinShiHuang(context);
    }
    private void reportToQinShiHuang(String reportContext){
        System.out.println("李斯:报告,秦老板!韩非子有活动了--->"+reportContext);
    }
}
public class Watch extends Thread{
    private HanFeiZi hanFeizi;
    private LiSi liSi;
    private String type;
    public Watch(HanFeiZi hanFeiZi,LiSi liSi,String type){
        this.hanFeizi = hanFeiZi;
        this.liSi = liSi;
        this.type = type;
    }
    @Override
    public void run(){
        while(true){
            try{
                Thread.sleep(10);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
            //监控是否在吃早餐
            if( "breakfast".equals(type)){
                //如果发现韩非子在吃饭,就通知李斯
                if(hanFeizi.isHavingBreakfast()) {
                    liSi.update("韩非子在早餐");
                    hanFeizi.setHavingBreakfast(false);
                }
            }else {
                if(this.hanFeizi.isHavingFun()){
                    liSi.update("韩非子在娱乐");
                    hanFeizi.setHavingFun(false);
                }
            }
        }
    }
}
public static void main(String[] args) {
        LiSi liSi = new LiSi();
        HanFeiZi hanFeizi = new HanFeiZi();
        //观察早餐
        Watch watchBreakfast = new Watch(hanFeizi,liSi,"breakfast");
        watchBreakfast.start();
        Watch watchFun = new Watch(hanFeizi,liSi,"fun");
        watchFun.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        hanFeizi.haveBreakfast();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        hanFeizi.haveFun();
    }
韩非子:开始吃饭了...
李斯:观察到韩非子活动,开始向老板汇报了。。。
李斯:报告,秦老板!韩非子有活动了--->韩非子在早餐
韩非子:开始娱乐了...
李斯:观察到韩非子活动,开始向老板汇报了。。。
李斯:报告,秦老板!韩非子有活动了--->韩非子在娱乐

观察者模式_第2张图片

你可能感兴趣的:(观察者模式)