观察者模式定义了一系列的对象之间的一对多的关系。
当一个对象改变状态的时候,其他依赖者都会受到通知。
观察者模式可以比作是报纸出版商(WeatherData )和订阅者(interface Observer,CurrentConditionsDisplay ,StatisticsDisplay,ForecastDisplay )之间的关系。
如果订阅者需要收到新的报纸(temperature,humidity,pressure),则需要向报社订阅报纸(public void registerObserver(Observer o)),此时报社会把订阅者的名字写在一个名单上( private List observers),这样每次都可以从报童(interface DisplayElement)根据报社提供的名单来派发报纸(public void notifyObserver()),订阅者可以每次从报童那里得到报纸,如果有一天订阅者不再需要报纸则报社会把订阅者的名字从订阅者的名单列表中删除(public void removeObserver(Observer o))。
package tablejava;
import java.util.ArrayList;
import java.util.List;
interface Subject{
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObserver();
}
interface Observer{
public void update(float temp,float humidity,float pressure);
}
interface DisplayElement{
public void display();
}
class WeatherData implements Subject{
private List observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
this.observers=new ArrayList();
}
@Override
public void registerObserver(Observer o) {
this.observers.add(o);
}
@Override
public void removeObserver(Observer o) {
int i=observers.indexOf(o);
if(i>0) {
observers.remove(o);
}
}
@Override
public void notifyObserver() {
for(int i=0;i<this.observers.size();i++) {
Observer observer=(Observer)observers.get(i);
observer.update(this.temperature, this.humidity, this.pressure);
}
}
public void measurementsChanged() {
notifyObserver();
}
public void setMeasurements(float temperature,float humidity,float pressure) {
this.temperature=temperature;
this.humidity=humidity;
this.pressure=pressure;
measurementsChanged();
}
}
class CurrentConditionsDisplay implements Observer,DisplayElement{
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData) {
super();
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void display() {
System.out.println("Current conditions:"+temperature+" F degrees and "+humidity+"% humidity");
}
@Override
public void update(float temp, float humidity, float pressure) {
this.temperature=temp;
this.humidity=humidity;
this.display();
}
}
class StatisticsDisplay implements Observer,DisplayElement{
private float temperature;
private float humidity;
private Subject weatherData;
public StatisticsDisplay(Subject weatherData) {
super();
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void display() {
System.out.println("Statistics conditions:"+temperature+" F degrees and "+humidity+"% humidity");
}
@Override
public void update(float temp, float humidity, float pressure) {
this.temperature=temp;
this.humidity=humidity;
this.display();
}
}
class ForecastDisplay implements Observer,DisplayElement{
private float temperature;
private float humidity;
private Subject weatherData;
public ForecastDisplay(Subject weatherData) {
super();
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void display() {
System.out.println("Statistics conditions:"+temperature+" F degrees and "+humidity+"% humidity");
}
@Override
public void update(float temp, float humidity, float pressure) {
this.temperature=temp;
this.humidity=humidity;
this.display();
}
}
public class ObserverMethod {
public static void main(String[] args) {
WeatherData weatherData=new WeatherData();
CurrentConditionsDisplay currentDisplay=new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay=new StatisticsDisplay(weatherData);
ForecastDisplay forecastDispaly=new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(81, 66, 31.4f);
weatherData.setMeasurements(82, 67, 32.4f);
}
}
运行结果:
Current conditions:80.0 F degrees and 65.0% humidity
Statistics conditions:80.0 F degrees and 65.0% humidity
Statistics conditions:80.0 F degrees and 65.0% humidity
Current conditions:81.0 F degrees and 66.0% humidity
Statistics conditions:81.0 F degrees and 66.0% humidity
Statistics conditions:81.0 F degrees and 66.0% humidity
Current conditions:82.0 F degrees and 67.0% humidity
Statistics conditions:82.0 F degrees and 67.0% humidity
Statistics conditions:82.0 F degrees and 67.0% humidity
这是上面的代码的逻辑: