参考Head First 设计模式
观察者模式:定义对象之间一对多的依赖,当“一”发生改变时,所有的依赖者都会收到通知并自动改变,主题与订阅者关系。
定义主题接口Subject
/**
* @description:
* @author: zhangyh
* @create :2018/6/2
*/
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
定义观察者接口Observer
/**
* @description:
* @author: zhangyh
* @create :2018/6/2
*/
public interface Observer {
public void update(float temp,float humidity,float pressure);
}
定义展示面板接口
/**
* @description:
* @author: zhangyh
* @create :2018/6/2
*/
public interface DisplayElement {
public void display();
}
定义主题实现subject接口
/**
* @description:
* @author: zhangyh
* @create :2018/6/2
*/
public class WeatherData implements Subject {
private ArrayList<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData(){
observers = new ArrayList<>();
}
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >=0 ){
observers.remove(i);
}
}
@Override
public void notifyObservers() {
for (int i =0 ;i < observers.size() ;i++){
Observer observer = observers.get(i);
observer.update(temperature,humidity,pressure);
}
}
public void messurementChanged(){
notifyObservers();
}
public void setMessurements(float temperature,float humidity,float pressure){
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
messurementChanged();
}
}
定义观察者实现,观察者接口,展示接口
/**
* @description:
* @author: zhangyh
* @create :2018/6/2
*/
public class CurrentConditionsDisplay implements Observer,DisplayElement {
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
@Override
public void display() {
System.out.println( "CurrentConditionsDisplay{" +
"temperature=" + temperature +
", humidity=" + humidity +
'}');
}
}
观察者2
/**
* @description:
* @author: zhangyh
* @create :2018/6/2
*/
public class ForecastConditionsDisplay implements Observer,DisplayElement {
private float temperature;
private float humidity;
private Subject weatherData;
public ForecastConditionsDisplay(Subject weatherData){
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
@Override
public void display() {
System.out.println( "ForecastConditionsDisplay{" +
"temperature=" + temperature +
", humidity=" + humidity +
'}');
}
}
测试
/**
* @description:
* @author: zhangyh
* @create :2018/6/2
*/
public class WeatherDataStation {
public static void main(String[] args){
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
ForecastConditionsDisplay forecastConditionsDisplay = new ForecastConditionsDisplay(weatherData);
weatherData.setMessurements(80,66,30.4F);
weatherData.setMessurements(80,67,30.5F);
weatherData.setMessurements(80,68,30.6F);
}
}
当主题发生变化时通知各个订阅者。