您的位置:首页 > 编程语言 > Java开发

(java)从零开始之--观察者设计模式Observer

2016-05-09 23:28 495 查看
观察者设计模式:时当一个对象发生指定的动作时,要通过另外的对象做出相应的处理。

步骤:
1. A对象发生指定的动作是,要通知B,C,D...对象做出相应的处理,这时候应该把B,C,D...对象针对A对象的动作做出的相应处理方法定义在接口上(这是一种规范,凡事需要A对象通知的对象,都要实现该接口)。
2. 在A对象维护接口的引用,当A对象发生指定的动作这时候即可调用接口中的方法。

观察者模式的应用场景:

1. 对一个对象状态的更新,需要其他对象同步更新,而且其他对象的数量动态可变。

2.对象仅需要将自己的更新通知给其他对象而不需要知道其他对象的细节。

假设有一个例子:

气象台每天都会更新天气预报,订阅的人群都可以在每次气象台发布天气预报,都可以接收到天气预报信息.订阅人群可以根据天气预报信息做出相应处理

被观察对象(Subject)

/*
* 气象台类
*
*/
public class WeatherStation {

private String[] weatherArr = {"大雨","大雪","台风","雾霾","多云","晴"};
private String weatherDesc = weatherArr[0];
private ArrayList<IWeather> list = new ArrayList<IWeather>();

//对外提供一个方法添加天气预报更新时,要通知的对象
public void add(IWeather weather){
this.list.add(weather);
}

//对外提供一个方法取消订阅天气预报
public void remove(IWeather weather){
if(weatherArr.length>0){
for (int i = 0; i < weatherArr.length; i++) {
if(list.get(i).equals(weather)){
this.list.remove(i);
break;
}
}

}
}

//开始天气预报
public void startWeatherForecast(){
new Thread(){
@Override
public void run() {
while(true){
updateWeather();//每3秒钟更新一次天气预报 (模拟每天气象台自动更新天气预报)
for (IWeather weather : list) {
weather.updateWeather(weatherDesc);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
}.start();

}

//气象台更新天气
public void updateWeather(){
Random random = new Random();
int n = random.nextInt(weatherArr.length);
this.weatherDesc = weatherArr
;
System.out.println("气象台发布天气预报:"+this.weatherDesc);
}

}


所有订阅的群体必须具有一个共同updateWeather的方法(实现该接口),这样气象台发布新天气预报时候,即可通过调用订阅对象执行该方法,达到通知目的。

/*
* 一个接口规范,实现了该接口对象所要做的动作。
* 实现该接口的对象当气象台天气预报更新时执行
* {"大雨","大雪","台风","雾霾","多云","晴"}
*/
public interface IWeather {
public void updateWeather(String weatherDesc);
}


订阅的群体类 观察者对象Observer

/*
* 可能订阅群个之一,工作者
*
*/
public class Emp implements IWeather {

String name;
int age;

public Emp(String name, int age) {
this.name = name;
this.age = age;
}

//{"大雨","大雪","台风","雾霾","多云","晴"}
@Override
public void updateWeather(String weatherDesc) {
System.out.println(this.name+":收到天气预报 "+weatherDesc);
}
}


-

/*
* 可能订阅群体之一, 学生
*/
public class Student implements IWeather {

String name;
int age;

public Student(String name, int age) {
this.name = name;
this.age = age;
}

//{"大雨","大雪","台风","雾霾","多云","晴"}
@Override
public void updateWeather(String weatherDesc) {
System.out.println(this.name+":收到天气预报 "+weatherDesc);
}

}


Main、

public class WeatherMain {

public static void main(String[] args) {
WeatherStation weatherStation = new WeatherStation();

Student s1 = new Student("小明", 10);
Student s2 = new Student("小美", 10);
Emp e1 = new Emp("大明", 10);
Emp e2 = new Emp("大美", 10);
weatherStation.add(s1);
weatherStation.add(s2);
weatherStation.add(e1);
weatherStation.add(e2);
weatherStation.remove(e2);
weatherStation.startWeatherForecast();
}

}


因为“大美”取消了天气预报,所以没有收到天气预报通知

执行结果:

气象台发布天气预报:大雪
小明:收到天气预报 大雪
小美:收到天气预报 大雪
大明:收到天气预报 大雪
气象台发布天气预报:雾霾
小明:收到天气预报 雾霾
小美:收到天气预报 雾霾
大明:收到天气预报 雾霾
气象台发布天气预报:台风
小明:收到天气预报 台风
小美:收到天气预报 台风
大明:收到天气预报 台风
气象台发布天气预报:晴
小明:收到天气预报 晴
小美:收到天气预报 晴
大明:收到天气预报 晴
气象台发布天气预报:雾霾
小明:收到天气预报 雾霾
小美:收到天气预报 雾霾
大明:收到天气预报 雾霾

...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: