观察者模式

观察者设计模式;当一个对象发生指定的动作时要通知另外一个对象作出相应的处理。Swing中事件监听就是典型的观察者设计模式。

 

经典案例:天气预报

观察者设计模式其实就是一种面向接口的编程方式,观察者设计模式的步骤:

1、当目前对象发生制定动作时,要通知另外一个对象作出相应的处理,这时候应该对方的相应的处理方法定义在接口中。

2、在当前对象维护接口的引用,当前对象发生指定的动作时,即可调用接口中的方法。

3、被通知者所属的类,都应当实现那个接口,这个接口相当于一个中介。

好好理解,观察者模式,以后的使用很多的。

 

  1 //天气接口:
  2 public interface Weather {
  3     public void notifyWeather(String weather);    // weather接口中只有这么一个接受天气的方法
  4 }
  5 //气象站:
  6 public class WeatherStation {
  7     String[] weathers = {"晴天", "雨天", "刮风", "下雪", "冰雹"};
  8     
  9     //当前的天气状况
 10     String weather = null;
 11     
 12     //谁订阅了天气预报,就添加到这里面
 13     ArrayList<Weather> list = new ArrayList<Weather>();
 14     //添加
 15     public void addListener(Weather w)
 16     {
 17         list.add(w);
 18     }
 19     
 20     
 21     //更新天气
 22     public void updateWether()
 23     {
 24         Random random = new Random();
 25         int index = random.nextInt(5);
 26         weather = weathers[index];
 27     }
 28     
 29     //开始工作
 30     public void startWork(){
 31         //工作应当是持续不断的 还有给别的携程工作的时间,所以应当使用多线程
 32         final Random random = new Random();
 33         new Thread()
 34         {
 35             @Override
 36             public void run() {
 37                 while(true)
 38                 {
 39                     updateWether();
 40                     
 41                     for (Weather w : list) {
 42                         w.notifyWeather(weather);
 43                     }
 44                     
 45                     //1-1.5s更新一次
 46                     int s = random.nextInt(501)+1000;
 47                     try {
 48                         Thread.sleep(s);
 49                     } catch (InterruptedException e) {
 50                         // TODO Auto-generated catch block
 51                         e.printStackTrace();
 52                     }
 53                 }
 54             }
 55         }.start();
 56     }
 57 }
 58 //学生类:
 59 public class Student implements Weather{
 60     String name;
 61 
 62     public Student(String name) {
 63         this.name = name;
 64     }
 65 
 66     @Override
 67     //"晴天", "雨天", "刮风", "下雪", "冰雹"
 68     public void notifyWeather(String weather) {
 69         // TODO Auto-generated method stub
 70         if("晴天".equals(weather))
 71         {
 72             System.out.println(this.name+weather+" 上学去");
 73         }else if("雨天".equals(weather))
 74         {
 75             System.out.println(this.name+weather+" 带上伞");
 76         }else if("刮风".equals(weather))
 77         {
 78             System.out.println(this.name+weather+" 穿厚一点");
 79         }
 80         else if("下雪".equals(weather))
 81         {
 82             System.out.println(this.name+weather+" 不骑车");
 83         }else if("冰雹".equals(weather))
 84         {
 85             System.out.println(this.name+weather+" 不出门");
 86         }    
 87     }    
 88 }
 89 //驱动程序:
 90 public class WeatherMain {
 91     public static void main(String[] args) {
 92         WeatherStation station = new WeatherStation();
 93         
 94         Student s1 = new Student("小名");
 95         Student s2 = new Student("小红");
 96         
 97         station.addListener(s1);
 98         station.addListener(s2);
 99         
100         station.startWork();
101     }
102 }

 

posted @ 2016-11-03 16:50  zhangoliver  阅读(219)  评论(0)    收藏  举报