今天看了一下设计模式中的观察者模式,理解的不算太深但是把例子写了一下,还算有些收获吧。
简介:观察者设计模式定义了对象间的一种一对多的依赖关系,以便一个对象的状态发生变化时,所有依赖于它的对象都得到通知并自动刷新。
用大白话来说就是:你们都关注我,我给你们发送通知,【你们<=>观察者】【发通知人<=>主题】
举例子一个生活中的例子来说:
就类似订阅报纸,你订阅一家报社的报纸,【报社就是主题、你本人就是观察者】当报社有新的报纸会发送给订阅者。等价过来观察者模式就是,当主题信息改变或者更新后,会为观察者发送最先信息,观察者获取最新的信息。
例子:气象站
描述:此例子中的三个部分是气象站(获取实际气象数据的物理装置)、WeatherData对象【相当于报社的意义】(追踪来自气象站的数据,并更新布告板)和 布告板【相当于订阅者的意义】可以有很多很多个(显示目前天气状况给用户看)。
接口3个(Observer、Subject、DisplayElement)
主题类1个(WeatherData)
观察者类3个(StatisticsDisplay、ForecastDisplay、CurrentConditionDisplay)
主类1个(WeatherStation)
下面给张图片屡屡思路【pdf图片太模糊了,自己就拍了一个】
1、接口类文件
//主题需要实现的接口
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
//观察者需要实现的接口1
public interface Observer {
public void update(float temp,float humidity,float pressure);
}
//观察者需要实现的接口2
public interface DisplayElement {
public void display();
}
2、3个布告板类(StatisticsDisplay.java、ForecastDisplay.java、CurrentConditionDisplay.java)
public class CurrentConditionDisplay implements Observer,DisplayElement{
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure) {
this.temperature = temp;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "%humidity");
}
}
public class ForecastDisplay implements Observer,DisplayElement{
private float temperature;
private float humidity;
private Subject weatherData;
public ForecastDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure) {
this.temperature = temp;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("ForecastDisplay: " + temperature + "F degrees and " + humidity + "%humidity");
}
}
public class StatisticsDisplay implements Observer,DisplayElement{
private float temperature;
private float humidity;
private Subject weatherData;
public StatisticsDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure) {
this.temperature = temp;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("StatisticsDisplay: " + temperature + "F degrees and " + humidity + "%humidity");
}
}
3、主题类文件
import java.util.ArrayList;
public class WeatherData implements Subject{
private ArrayList observeres;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
observeres = new ArrayList();
}
public void registerObserver(Observer o) {
observeres.add(o);
}
public void removeObserver(Observer o) {
int i = observeres.indexOf(o);
if(i>0){
observeres.remove(i);
}
}
public void notifyObservers() {
for (int i = 0; i < observeres.size(); i++) {
Observer observer = (Observer)observeres.get(i);
observer.update(temperature, humidity, pressure);
}
}
public void measurementsChanged(){
notifyObservers();
}
public void setMeasurements(float temperature, float humidity, float pressure){
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}4、主类
import java.io.StreamTokenizer;
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionDisplay currentDisplay = new CurrentConditionDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(40, 65, 30.4f);
}
}
最后运行结果:

1371

被折叠的 条评论
为什么被折叠?



