观察者模式(转)

 

 1 package cn.itcast.observer;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Random;
 5 /*
 6  观察者设计模式:观察者设计模式解决的问题时当一个对象发生指定的动作时,要通过另外一个对象做出相应的处理。
 7  
 8 需求: 编写一个气象站、一个工人两个类,当气象站更新天气 的时候,要通知人做出相应的处理。
 9   
10 
11  问题1: 气象站更新了多次天气,然后人才做一次的处理。
12  
13 问题2: 目前气象站只能通知一个人而已。
14 
15 问题3: 在现实生活中出了工人群体要关注天气,其他 的群体也需要关注天气
16 
17 
18 观察者设计模式的步骤:
19     1. 当前目前对象发生指定的动作是,要通知另外一个对象做出相应的处理,这时候应该把对方的相应处理方法定义在接口上。
20     2. 在当前对象维护接口的引用,当当前对象发生指定的动作这时候即可调用接口中的方法了。
21 
22 
23  */
24 
25 //气象站
26 public class WeatherStation {
27     
28     String[] weathers = {"晴天","雾霾","刮风","冰雹","下雪"};
29     
30     //当前天气
31     String  weather ;
32     
33     //该集合中存储的都是需要收听天气预报的人
34     ArrayList<Weather> list = new ArrayList<Weather>();  //程序设计讲究低耦合---->尽量不要让一个类过分依赖于另外一个类。
35     
36     
37     
38     public void addListener(Weather e){
39         list.add(e);
40     }
41     
42     
43     //开始工作
44     public void startWork() {
45         final Random random = new Random();
46         
47         new Thread(){    
48             @Override
49             public void run() {
50                 while(true){ 
51                     updateWeather(); // 每1~1.5秒更新一次天气  1000~1500
52                     for(Weather e : list){
53                         e.notifyWeather(weather);
54                     }
55 
56                     int  s = random.nextInt(501)+1000; //  500
57                     try {
58                         Thread.sleep(s);
59                     } catch (InterruptedException e) {
60                         e.printStackTrace();
61                     }
62                 }                
63             }
64             
65         }.start();
66         
67     }
68     
69     
70     
71     //更新天气的 方法
72     public void updateWeather(){
73         Random random = new Random();
74         int index = random.nextInt(weathers.length);
75         weather = weathers[index];
76         System.out.println("当前的天气是: " + weather);
77     }
78     
79 }

 

 

1 package cn.itcast.observer;
2 
3 //订阅天气预报的接口
4 public interface Weather {
5     
6     
7     public void notifyWeather(String weather);
8 
9 }
 1 package cn.itcast.observer;
 2 
 3 import java.util.Random;
 4 
 5 public class WeatherMain {
 6 
 7     public static void main(String[] args) throws Exception {
 8         //工人
 9         Emp e = new Emp("小明");
10         Emp e2 = new Emp("如花");
11         
12         //学生
13         Student s1 = new Student("狗娃");
14         Student s2 = new Student("狗剩");
15         
16         
17         WeatherStation station = new WeatherStation();
18         station.addListener(e);
19         station.addListener(e2);
20         station.addListener(s1);
21         station.addListener(s2);
22         
23         
24         
25         station.startWork();
26         
27         
28     }
29     
30 }
package cn.itcast.observer;

//人 是要根据天气做出相应的处理的。
public class Emp implements Weather{
    
    String name;

    public Emp(String name) {
        this.name = name;
    }
    
    
    //人是要根据天气做出相应的处理的。  "晴天","雾霾","刮风","冰雹","下雪"
    public void notifyWeather(String weather){
        if("晴天".equals(weather)){
            System.out.println(name+"高高兴兴的去上班!!");
        }else if("雾霾".equals(weather)){
            System.out.println(name+"戴着消毒面具去上班!");
        }else if("刮风".equals(weather)){
            System.out.println(name+"拖着大石头过来上班!");
        }else if("冰雹".equals(weather)){
            System.out.println(name+"戴着头盔过来上班!");
        }else if("下雪".equals(weather)){
            System.out.println(name+"戴着被子过来上班!");
        }
        

    }

}
 1 package cn.itcast.observer;
 2 
 3 public class Student implements Weather{
 4     
 5     String name;
 6 
 7     public Student(String name) {
 8         super();
 9         this.name = name;
10     }
11     
12     
13     public void notifyWeather(String weather){
14         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         }else if("下雪".equals(weather)){
23             System.out.println(name+"等下完再去上学!");
24         }
25         
26 
27     }
28     
29 }

 

posted @ 2016-06-18 15:25  damon2016  阅读(258)  评论(0编辑  收藏  举报