package cn.itcast.observer;
//人 是要根据天气做出相应的处理的。"晴天","雾霾","刮风","冰雹","下雪"
//天气怎么来呢?
//public class Emp {
public class Emp implements Weather{
String name;
public Emp(String name) {
super();
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+"戴着被子过来上班!!!!");
}
}
}
package cn.itcast.observer;
public class Student implements Weather{
String name;
public Student(String name) {
super();
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+"等下完再去上学!!!!");
}
}
}
package cn.itcast.observer;
//订阅天气预报的接口...
//谁都可以收听天气预报,谁想收听就来实现我这个接口.我定义规则,你们照着走.
//接口在这里就充当了一个中介的模式了.特别是安卓用的特别的多.做一个下载,下载完之后要给某某地方发送一个通知.
//这时候就要想到观察者设计模式,想到我们的接口的实现.
public interface Weather {
public void notifyWeather(String weather);//这个接口里面就维护着一个方法.
}
package cn.itcast.observer;
import java.util.ArrayList;
import java.util.Random;
/*
*
* 观察者设计模式: 观察者设计模式解决的问题时当一个对象发生指定的动作时,要通过另外一个对象做出相应的处理。
*
* 需求:天气预报的.编写一个气象站、一个工人两个类,当气象站更新天气的时候,要通知人做出相应的处理.
*
* 问题1:气象站更新了多次天气,然后人才做一次的处理.
*
* 问题2:目前气象站只能通知一个人而已。
*
* 问题3:在现实生活中除了工人群体要关注天气,其他的群体也需要关注天气的.
* 事件监听,当一个按钮被点击的时候,它就会调用到一个对应的方法。
* 观察者设计模式的步骤:
* 1. 当前目前对象发生指定的动作是,要通知另外一个对象做出相应的处理,这时候应该把对方的相应处理方法定义在接口上.这时候定义一个接口,把要通知它的一个行为定义成一个接口.把这种行为声明在一个接口上面.
* 2. 在当前对象维护接口的引用,当当前对象发生指定的动作这时候可调用接口中的方法了。
*
*/
//气象站
public class WeatherStation {
String[] weathers = {"晴天","雾霾","刮风","冰雹","下雪"};
//当前天气
String weather;
/*
//人
Emp e;
Emp e2;//不断地声明变量来维护肯定是不行的.
*/
//人
//用集合来传很多人进去.
//该集合中存储的都是需要收听天气预报的人
//ArrayList<Emp> list = new ArrayList<Emp>();
//ArrayList<Object> list = new ArrayList<Object>(); //程序设计讲究低耦合----->尽量不要让一个类过分依赖于另外一个类..
//现在WeatherStation非常依赖Emp.如果某一天Emp被删除掉了,WeatherStation就出问题了.这两个类之间的耦合性是非常高的.
//程序设计要低耦合,再维护多一个集合也是于事无补的东西.可以再搞多一个对象的.
//中国移动不会给每个人发天气预报,短信回复XX信息给10086订阅天气预报,中国移动就会每个月给你发天气预报.这个规则是移动来定的.
//气象站要主动地通知人来更新天气.气象站有资格能定义一些订阅天气预报的规则.作为发布者肯定有资格定一些规则.谁想收听天气预报就要遵守我的规则.
//就像中国移动,想收听天气预报就要遵守移动的规则,发送xxxxx到10086.这个时候接口就派上用场了.
//如果这里多维护一个学生的集合,程序的扩展性不高,维护非常不方便...
ArrayList<Weather> list = new ArrayList<Weather>();//传入接口做参数 这就是接口的好处 解耦
//你是写WeatherStation的人,那么你就有资格定义规则给Emp或者Student走.这个接口不会是其他人去写的.你应该不会自己害自己吧.
//这就是程序的解耦.用上了这个接口给它们定义了一个规范之后,即使你多加一个群体,
/* public WeatherStation(Emp e) {
this.e = e;
}*/
/* public void addListener(Emp e){
list.add(e);
}*/
public void addListener(Weather w){
list.add(w);
}
//开始工作
public void startWork() throws InterruptedException{
//不同代码同时执行
new Thread(){
@Override
public void run() {
//有一段线程单独服务这一段代码了.
while(true){
Random random = new Random();
updateWeather();//每1~1.5秒更新一次天气 1000~1500
//天气更新的时候,遍历这个集合,挨个挨个地通知他们
/* for (Emp e : list) {
//for (Object e : list) {
e.notifyWeather(weather);
}
*/
for (Weather w : list) {
//for (Object e : list) {
w.notifyWeather(weather);
}
//e.notifyWeather(weather);
int s =random.nextInt(501)+1000; //0~500+1000=1500
//假如这里产生的随机数是500,那边产生的随机数是1500,发生随机数不一致的问题
try {
Thread.sleep(s);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
//为这段代码分配一段线程
/* while(true){
Random random = new Random();
updateWeather();//每1~1.5秒更新一次天气 1000~1500
int s =random.nextInt(501)+1000; //0~500+1000=1500
Thread.sleep(s);
}*/
}
//更新天气的方法
public void updateWeather(){
Random random = new Random();
int index = random.nextInt(weathers.length);
weather = weathers[index];
System.out.println("当前的天气是:"+ weather);
}
}
package cn.itcast.observer;
import java.util.Random;
public class WeatherMain {
public static void main(String[] args) throws Exception {
//WeatherStation station = new WeatherStation();
//工人
Emp e = new Emp("小明");
Emp e2 = new Emp("如花");
//学生
Student s1 = new Student("狗娃");
Student s2 = new Student("狗剩");
//WeatherStation station = new WeatherStation(e);
WeatherStation station = new WeatherStation();
station.addListener(e);
station.addListener(e2);
station.addListener(s1);
station.addListener(s2);
/*station.addListener(s1);
station.addListener(s2);*/
//Object更恶心,如果传个狗给你呢?气象站会通知狗的吗?
//station.addListener(new Dog());
station.startWork();//因为startWork()是while(true),startwWork()永远都干不完,代码永远都下不来
/* //工人
Emp e = new Emp("小明");
//e.notifyWeather(station.weather);
//气象站不断地更新天气,人也要不断地关注天气,做出相应的处理。
Random random = new Random();
while(true){//不停地关注,关注是不是有点频繁啊。
e.notifyWeather(station.weather);
int s = random.nextInt(501)+1000;//这边产生的随机数是1500的话
Thread.sleep(s);
}
*/
//应该是由气象站主动地通知人的,而不是由人主动地关注天气的.
//更不更新天气气象站最清楚.气象站广播天气.
}
}