java 观察者模式

</pre></p><p>     1 首先介绍下观察者模式 </p><p></p><p>     </p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">    <strong>观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式、模型-视图(Model/View)模式、源-监听器(Source/Listener)模式或从属者(Dependents)模式。</strong></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px"><strong>  观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。</strong></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px"><strong>      举个例子会比较好理解就是,好比医生要在病人有情况的时候做出不同的动作,那么医生要监听病人的状态,病人转好或者转差,医生就会根据实际情况做出反应,并进行相应的治疗。</strong></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px"><strong></strong></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px"><strong>      </strong></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">  观察者模式所涉及的角色有:</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">  ●  <strong>抽象主题(Subject)角色:</strong>抽象主题角色把所有对观察者对象的引用保存在一个聚集(比如ArrayList对象)里,每个主题都可以有任何数量的观察者。抽象主题提供一个接口,可以增加和删除观察者对象,抽象主题角色又叫做抽象被观察者(Observable)角色。</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">  ●  <strong>具体主题(ConcreteSubject)角色:</strong>将有关状态存入具体观察者对象;在具体主题的内部状态改变时,给所有登记过的观察者发出通知。具体主题角色又叫做具体被观察者(Concrete Observable)角色。</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">  ●  <strong>抽象观察者(Observer)角色:</strong>为所有的具体观察者定义一个接口,在得到主题的通知时更新自己,这个接口叫做更新接口。</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">  ●  <strong>具体观察者(ConcreteObserver)角色:</strong>存储与主题的状态自恰的状态。具体观察者角色实现抽象观察者角色所要求的更新接口,以便使本身的状态与主题的状态 像协调。如果需要,具体观察者角色可以保持一个指向具体主题对象的引用。</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">       比如病人觉得痛,他会通知医生过来,医生要做什么,那就是医生要判断的了。</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px"></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">   下面实现下,病人痛了,渴了,通知医生,然后医生做出他应该的动作。</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">      首先我们先分析下,病人就是被观察者,他的身体状态有变化就会通知观察者(医生)给他治疗。</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px"></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">       首先定义一个病人的抽象体:</p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px"></p><p style="word-wrap:break-word; margin:10px auto; font-family:'ms shell dlg'; font-size:14px; line-height:28px">    <pre name="code" class="java">public abstract class SickPersion {

    
    /**
     * 用来保存注册的观察者对象(就是要注意病人病情的医生)
     */
    private    List<Doctor> list = new ArrayList<Doctor>();
    
    
    /**
     * 注册观察者对象  ,就是增加医生,说明病情不容乐观
     * @param doctor    医生对象
     */
    public void  addDocotor(Doctor doctor){
        
        list.add(doctor);
        System.out.println("add a doctor");
    }
    
    /**
     * 删除观察者就是减少医生,说明病情好转
     * @param doctor 医生对象,
     */
    
    public void deleteDocotor(Doctor doctor){
        list.remove(doctor);
        System.out.println("minor a doctor");
    }
    
    
    /**
     * 通知所有注册的观察者对象
     */
    public void nodifyDoctors(String newState){
        
        for(Doctor doctor : list){
            doctor.diagnosis(newState);
        }
    }
    
}

  现在好比有一个具体的病人person1

public class Person1  extends SickPersion{
    
  private String state;
    
    public String getState() {
        return state;
    }

    public void change(String newState){
        state = newState;
        System.out.println("病人现在感觉:" + state);
        //状态发生改变,通知各个观察者
        this.nodifyDoctors(newState);
    }
}

有一个观察者的抽象(医生的抽象)

public abstract class Doctor {

    /**
     * 诊断
     */
    public void  diagnosis(String newState) {
          
    }
}
我们先定义一个医生王,主要是医牙的

public class DoctorWang  extends Doctor{

    /**
     * 诊治牙痛
     */
    public void  diagnosis(String newState) {
          
         System.out.println("王医生的处理:给病人拔掉痛的那颗牙");
    }
}


最后客户端使用的时候可以定义如下:

public class Client {

    public static void main(String [] args){
        
        Person1 person1 = new Person1();
        DoctorWang doctorWang = new DoctorWang();
        
        person1.addDocotor(doctorWang);
        person1.change("牙痛");
   

    }
}

运行结果:

add a doctor
病人现在感觉:牙痛
王医生的处理:给病人拔掉痛的那颗牙



这个观察者模型里面我们发现其实观察者们 要知道被观察者的状态,从而通过这些状态来过滤自己要使用的,也可以通过配置来要观察者们来订阅自己需要的状态变化。






      

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值