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

      有时被称作发布/订阅模式,观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。

      比如你有一个气象站,隔一段时间检测一次天气,检测完了需要通知有需求的人群,这些人群会即时根据天气做出相应的反应。而由于这些人群的属性不同,所以不能用一个类。此时,观察者设计模式就可以用了。

      首先设计一个接口,封装的方法作用就是通知天气,并且做出相应的反应。

      然后每一个有需求的人群类都继承和实现这个接口。

      最后就是在气象站类中添加一个元素为接口类型的list,由于有需求的人群都继承了这个接口,所以不论是什么类型的人群,都可以添加到中一个list中,然后遍历通知就ok了

    代码如下:

    接口类

    1 public interface Weather {
    2     public void notifyWeather(String weather);
    3 }

    Emp.java 人群1

     1 public class Emp implements Weather{
     2     
     3     String name;
     4 
     5     public Emp(String name) {
     6         this.name = name;
     7     }
     8     
     9     
    10     //人是要根据天气做出相应的处理的。  "晴天","雾霾","刮风","冰雹","下雪"
    11     public void notifyWeather(String weather){
    12         if("晴天".equals(weather)){
    13             System.out.println(name+"高高兴兴的去上班!!");
    14         }else if("雾霾".equals(weather)){
    15             System.out.println(name+"戴着消毒面具去上班!");
    16         }else if("刮风".equals(weather)){
    17             System.out.println(name+"拖着大石头过来上班!");
    18         }else if("冰雹".equals(weather)){
    19             System.out.println(name+"戴着头盔过来上班!");
    20         }else if("下雪".equals(weather)){
    21             System.out.println(name+"戴着被子过来上班!");
    22         }
    23         
    24 
    25     }
    26 
    27 }

    Student.java 人群2

     1 public class Student implements Weather{
     2     
     3     String name;
     4 
     5     public Student(String name) {
     6         super();
     7         this.name = name;
     8     }
     9     
    10     
    11     public void notifyWeather(String weather){
    12         if("晴天".equals(weather)){
    13             System.out.println(name+"高高兴兴的去开学!!");
    14         }else if("雾霾".equals(weather)){
    15             System.out.println(name+"吸多两口去上学!");
    16         }else if("刮风".equals(weather)){
    17             System.out.println(name+"在家睡觉!");
    18         }else if("冰雹".equals(weather)){
    19             System.out.println(name+"在家睡觉!");
    20         }else if("下雪".equals(weather)){
    21             System.out.println(name+"等下完再去上学!");
    22         }
    23         
    24 
    25     }
    26     
    27 }

    气象站类

     1 public class WeatherStation {
     2     
     3     String[] weathers = {"晴天","雾霾","刮风","冰雹","下雪"};
     4     
     5     //当前天气
     6     String  weather ;
     7     
     8     //该集合中存储的都是需要收听天气预报的人
     9     ArrayList<Weather> list = new ArrayList<Weather>();  //程序设计讲究低耦合---->尽量不要让一个类过分依赖于另外一个类。
    10     
    11     public void addListener(Weather e){
    12         list.add(e);
    13     }
    14     
    15     //开始工作
    16     public void startWork() {
    17         final Random random = new Random();
    18         
    19         new Thread(){    
    20             @Override
    21             public void run() {
    22                 while(true){ 
    23                     updateWeather(); // 每1~1.5秒更新一次天气  1000~1500
    24                     for(Weather e : list){
    25                         e.notifyWeather(weather);
    26                     }
    27 
    28                     int  s = random.nextInt(501)+1000; //  500
    29                     try {
    30                         Thread.sleep(s);
    31                     } catch (InterruptedException e) {
    32                         e.printStackTrace();
    33                     }
    34                 }                
    35             }
    36             
    37         }.start();
    38         
    39     }
    40     //更新天气的 方法
    41     public void updateWeather(){
    42         Random random = new Random();
    43         int index = random.nextInt(weathers.length);
    44         weather = weathers[index];
    45         System.out.println("当前的天气是: " + weather);
    46     }
    47     
    48 }

    主类:

     1 public class WeatherMain {
     2 
     3     public static void main(String[] args) throws Exception {
     4         //工人
     5         Emp e = new Emp("AA");
     6         Emp e2 = new Emp("BB");
     7         
     8         //学生
     9         Student s1 = new Student("CC");
    10         Student s2 = new Student("DD");
    11         
    12         WeatherStation station = new WeatherStation();
    13         station.addListener(e);
    14         station.addListener(e2);
    15         station.addListener(s1);
    16         station.addListener(s2);
    17             
    18         station.startWork();    
    19     }
    20     
    21 }
  • 相关阅读:
    redhat 5 中文乱码
    生成树
    交换机端口模式
    链路聚合
    AP注册
    信息收集
    Python 25 Django跨域请求
    Python 24 Django之csrf中间件
    Python 23 Django基础
    Python 21 Flask(三)第三方组件
  • 原文地址:https://www.cnblogs.com/K-artorias/p/7351806.html
Copyright © 2011-2022 走看看