package com.zyf.day27.guancha;
import java.util.ArrayList;
import java.util.Random;
/*
观察者设计模式:观察者设计模式解决的问题时当一个对象发生指定的动作时,要通过另外一个对象做出相应的处理。
需求: 编写一个气象站、一个工人两个类,当气象站更新天气 的时候,要通知人做出相应的处理。
问题1: 气象站更新了多次天气,然后人才做一次的处理。
问题2: 目前气象站只能通知一个人而已。
问题3: 在现实生活中出了工人群体要关注天气,其他 的群体也需要关注天气
观察者设计模式的步骤:
1. 当前目前对象发生指定的动作是,要通知另外一个对象做出相应的处理,这时候应该把对方的相应处理方法定义在接口上。
2. 在当前对象维护接口的引用,当当前对象发生指定的动作这时候即可调用接口中的方法了。
*/
//气象站
public class WeatherStation {
String[] weathers = {"晴天","雾霾","刮风","冰雹","下雪"};
//当前天气
String weather ;
//该集合中存储的都是需要收听天气预报的人
ArrayList<Weather> list = new ArrayList<Weather>(); //程序设计讲究低耦合---->尽量不要让一个类过分依赖于另外一个类。
public void addListener(Weather e){
list.add(e);
}
//开始工作
public void startWork() {
final Random random = new Random();
new Thread(){
@Override
public void run() {
while(true){
updateWeather(); // 每1~1.5秒更新一次天气 1000~1500
for(Weather e : list){
e.notifyWeather(weather);
}
int s = random.nextInt(501)+1000; // 500
try {
Thread.sleep(s);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//更新天气的 方法
public void updateWeather(){
Random random = new Random();
int index = random.nextInt(weathers.length);
weather = weathers[index];
System.out.println("当前的天气是: " + weather);
}
}
package com.zyf.day27.guancha;
//订阅天气预报的接口
public interface Weather {
public void notifyWeather(String weather);
}
package com.zyf.day27.guancha;
import java.util.Random;
public class WeatherMain {
public static void main(String[] args) throws Exception {
//工人
Emp e = new Emp("小明");
Emp e2 = new Emp("如花");
//学生
Student s1 = new Student("狗娃");
Student s2 = new Student("狗剩");
WeatherStation station = new WeatherStation();
station.addListener(e);
station.addListener(e2);
station.addListener(s1);
station.addListener(s2);
station.startWork();
}
}