zoukankan      html  css  js  c++  java
  • 观察者设计模式

    观察者设计模式

    此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。

    此种模式有时又被称为发布-订阅<Publish/Subscribe>模式、模型-视图<Model/View>模式、源-收听者<Source/Listener>模式或从属者<Dependents>模式)。

    观察者设计模式通常透过呼叫各观察者所提供的方法来实现。通常被用来实作事件处理系统。有多个观察者时,不可以依赖特定的通知次序。

    观察者设计模式的步骤:
        1. 当前对象发生指定的动作时,要通知另外一个对象做出相应的处理,这时候应该把对方的相应处理方法定义在接口上。
        2. 在当前对象维护接口的引用,当当前对象发生指定的动作这时候即可调用接口中的方法了。

    代码示例:

      1 //统一接口,来执行操作
      2 public interface Weather {
      3     
      4     public void notifyWeather(String weather);
      5 
      6 }
      7 
      8 //气象站
      9 public class WeatherStation {
     10     
     11     String[] weathers = {"晴天","雾霾","刮风","冰雹","下雪"};
     12     //当前天气
     13     String  weather ;
     14     //该集合中存储的都是需要收听天气预报的人
     15     ArrayList<Weather> list = new ArrayList<Weather>();  //程序设计讲究低耦合---->尽量不要让一个类过分依赖于另外一个类。
     16     
     17     public void addListener(Weather e){
     18         list.add(e);
     19     }
     20     
     21     //开始工作
     22     public void startWork() {
     23         final Random random = new Random();
     24         
     25         new Thread(){    
     26             @Override
     27             public void run() {
     28                 while(true){ 
     29                     updateWeather(); // 每1~1.5秒更新一次天气  1000~1500
     30                     for(Weather e : list){
     31                         e.notifyWeather(weather);
     32                     }
     33 
     34                     int  s = random.nextInt(501)+1000; //  500
     35                     try {
     36                         Thread.sleep(s);
     37                     } catch (InterruptedException e) {
     38                         e.printStackTrace();
     39                     }
     40                 }                
     41             }
     42             
     43         }.start();
     44         
     45     }
     46     
     47     //更新天气的 方法
     48     public void updateWeather(){
     49         Random random = new Random();
     50         int index = random.nextInt(weathers.length);
     51         weather = weathers[index];
     52         System.out.println("当前的天气是: " + weather);
     53     }
     54 }
     55 
     56 //学生类
     57 public class Student implements Weather{
     58     
     59     String name;
     60 
     61     public Student(String name) {
     62         super();
     63         this.name = name;
     64     }
     65     
     66     public void notifyWeather(String weather){
     67         if("晴天".equals(weather)){
     68             System.out.println(name+"高高兴兴的去开学!!");
     69         }else if("雾霾".equals(weather)){
     70             System.out.println(name+"吸多两口去上学!");
     71         }else if("刮风".equals(weather)){
     72             System.out.println(name+"在家睡觉!");
     73         }else if("冰雹".equals(weather)){
     74             System.out.println(name+"在家睡觉!");
     75         }else if("下雪".equals(weather)){
     76             System.out.println(name+"等下完再去上学!");
     77         }
     78     }
     79 }
     80 //工人类
     81 public class Emp implements Weather{
     82     
     83     String name;
     84     
     85     public Emp(String name) {
     86         this.name = name;
     87     }
     88     
     89     //人是要根据天气做出相应的处理的。  "晴天","雾霾","刮风","冰雹","下雪"
     90     public void notifyWeather(String weather){
     91         if("晴天".equals(weather)){
     92             System.out.println(name+"高高兴兴的去上班!!");
     93         }else if("雾霾".equals(weather)){
     94             System.out.println(name+"戴着消毒面具去上班!");
     95         }else if("刮风".equals(weather)){
     96             System.out.println(name+"拖着大石头过来上班!");
     97         }else if("冰雹".equals(weather)){
     98             System.out.println(name+"戴着头盔过来上班!");
     99         }else if("下雪".equals(weather)){
    100             System.out.println(name+"戴着被子过来上班!");
    101         }
    102     }
    103 }
    104 
    105 //程序主类
    106 public class WeatherMain {
    107     public static void main(String[] args) throws Exception {
    108         //工人
    109         Emp e = new Emp("小明");
    110         Emp e2 = new Emp("如花");
    111         //学生
    112         Student s1 = new Student("狗娃");
    113         Student s2 = new Student("狗剩");
    114         //天气站
    115         WeatherStation station = new WeatherStation();
    116         station.addListener(e);
    117         station.addListener(e2);
    118         station.addListener(s1);
    119         station.addListener(s2);
    120         station.startWork();
    121     }
    122 }
  • 相关阅读:
    docker镜像制作及上传到远端镜像仓库
    mysql索引进阶
    电子商务需要用到香港服务器吗?
    golang module goland 配置代理
    nginx做linux服务时,日志有权限提示没权限(nginx: [emerg] open() "/home/www/log/error.log" failed)
    Yaml 、Json 、Dict 之间的转化
    CodeSmith .NET三层架构模板
    C#获取26个英文字母
    基于PCASClass.js和layui.js的城市三级联动
    MySQL变量的使用
  • 原文地址:https://www.cnblogs.com/nicker/p/6274465.html
Copyright © 2011-2022 走看看