定义了对象之间的一对多的依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新.
一对多的关系
Java API已经为我们提供了相应的API
Observable类 ( 可观察者 只需要继承该类)
Observer接口 ( 被观察者 只需要实现该接口)
/** * 所有观察者必须实现这个观察者接口,当主题状态发送变化是会调用update()方法 */ public interface Observer { /** 当气象观察值改变时,主题会把这些状态值当做方法参数,传递给观察者 */ void update(float temp,float humidity,float perssure); }
/** 主题 */ public interface Subject { /** 注册观察者 **/ void registerObserver(Observer o); /** 删除观察者 **/ void removeObserver(Observer o); /** 通知观察者 **/ void notifyObservers(); }
public interface DisplayElement { /** 当布告板需要显示时,调用该方法 **/ void display(); }
/** 主题 **/ public class WetherData implements Subject { /** * 用于记录观察者,保存与观察者的关系 * 1(主题) 对 多(观察者) */ private List<Observer> observers; private float temprature; private float humidity; private float pressure; public WetherData() { 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); } } /** 在这里,我们把状态告诉每一个观察者,因为只要是观察者都实现了Observer接口 **/ @Override public void notifyObservers() { for (Object object : observers) { ((Observer)object).update(temprature, humidity, pressure);; } } /** 当从气象站得到更新天气时,我们就通知观察者 **/ public void measurementsChanged() { notifyObservers(); } /** **/ public void setMeasurements(float temprature,float humidity,float pressure) { this.temprature = temprature; this.humidity = humidity; this.pressure = pressure; } }
/** 某观察者 */ public class CurrentConditionsDisplay implements Observer, DisplayElement { private float temprature; private float humidity; /** * 观察者与主题的关系 * 多(观察者) 对 1(主题) * **/ @SuppressWarnings("unused") private Subject weaterDate; public CurrentConditionsDisplay(Subject weaterDate) { this.weaterDate = weaterDate; weaterDate.registerObserver(this); } @Override public void display() { System.err.println("Current conditions " + temprature + "F degrees and" + humidity + "% humidity"); } @Override public void update(float temp, float humidity, float perssure) { this.temprature = temp; this.humidity = humidity; display(); } }
public class WetherStation { public static void main(String[] args) { WetherData wetherData = new WetherData(); CurrentConditionsDisplay conditionsDisplay = new CurrentConditionsDisplay(wetherData); wetherData.setMeasurements(123, 2, 3); wetherData.notifyObservers(); conditionsDisplay.display(); } }
使用Java API更简单
/** 主题 **/ public class CurrentObserver implements Observer { Observable observable; private float temperature; private float humidity; @Override public void update(Observable o, Object arg) { if (o instanceof WeatherData) { WeatherData weatherData = (WeatherData) o; this.temperature = weatherData.getTemprature(); this.humidity = weatherData.getHumidity(); } } public void show() { System.err.println(" temperature = " + temperature); System.err.println(" humidity = " + humidity); } }
/** 观察者 **/ public class CurrentObserver implements Observer { Observable observable; private float temperature; private float humidity; @Override public void update(Observable o, Object arg) { if (o instanceof WeatherData) { WeatherData weatherData = (WeatherData) o; this.temperature = weatherData.getTemprature(); this.humidity = weatherData.getHumidity(); } } public void show() { System.err.println(" temperature = " + temperature); System.err.println(" humidity = " + humidity); } }
public class Main { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentObserver currentObserver = new CurrentObserver(); weatherData.addObserver(currentObserver); weatherData.setMeasurements(1, 2, 3); weatherData.measurementsChanged(); currentObserver.show(); } }