有时被称作发布/订阅模式,观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。
实现方式,用Java的内置的API实现观察者模式,主题对象继承Observable类,观察者对象实现Observer接口。
测试我们的观察者模式:
public class DogMain {
public static void main(String[] args) {
//被观察者
DogData dogData = new DogData();
//观察者对象
CurrentConditionsDisplay conditionsDisplay = new CurrentConditionsDisplay(dogData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(dogData);
ForecastDisplay forecastDisplay = new ForecastDisplay(dogData);
dogData.setMeasurements("乐乐", "白色", "柯基");
}
}
实现我们的主题类,也就是我们的被观察者:
public class DogData extends Observable{
private String name;
private String colour;
private String kinds;
public DogData() {
}
/**
* 被观察者数据发生改变
* @param name
* @param colour
* @param kinds
*/
public void setMeasurements(String name, String colour, String kinds) {
this.name = name;
this.colour = colour;
this.kinds = kinds;
this.measurementsChanged();
}
/**
* 修改后,通知观察者
*/
public void measurementsChanged() {
super.setChanged();
super.notifyObservers();
}
public String getName() {
return name;
}
public String getColour() {
return colour;
}
public String getKinds() {
return kinds;
}
}
定义一个公告板显示接口,观察者要实现此接口来显示在公告板上
public interface DisplayElement {
public void display();
}
Observable可以主动推(push)数据给观察者对象,也可以让观察者对象拉(pull)数据。
import java.util.Observable;
import java.util.Observer;
public class CurrentConditionsDisplay implements Observer, DisplayElement{
private String name;
private String colour;
private String kinds;
private Observable observable;
public CurrentConditionsDisplay(Observable observable) {
this.observable = observable;
observable.addObserver(this);
}
@Override
public void update(Observable o, Object arg) {
if(o instanceof DogData) {
this.name = ((DogData) o).getName();
this.colour = ((DogData) o).getColour();
this.kinds = ((DogData) o).getKinds();
this.display();
}
}
@Override
public void display() {
System.out.println("名字:"+ this.name+"\t"+"颜色:"+this.colour+"\t"+"品种:"+this.kinds);
}
}
import java.util.Observable;
import java.util.Observer;
public class ForecastDisplay implements Observer, DisplayElement{
private String name;
private String colour;
private String kinds;
private Observable observeable;
public ForecastDisplay(Observable observeable) {
this.observeable = observeable;
this.observeable.addObserver(this);
}
@Override
public void update(Observable o, Object arg) {
if(o instanceof DogData) {
this.name = ((DogData) o).getName();
this.colour = ((DogData) o).getColour();
this.kinds = ((DogData) o).getKinds();
this.display();
}
}
@Override
public void display() {
System.out.println("名字:"+ this.name+"\t"+"颜色:"+this.colour+"\t"+"品种:"+this.kinds);
}
}
import java.util.Observable;
import java.util.Observer;
public class StatisticsDisplay implements Observer, DisplayElement{
private String name;
private String colour;
private String kinds;
private Observable observable;
public StatisticsDisplay(Observable observable) {
this.observable = observable;
this.observable.addObserver(this);
}
@Override
public void update(Observable o, Object arg) {
if(o instanceof DogData) {
this.name = ((DogData) o).getName();
this.colour = ((DogData) o).getColour();
this.kinds = ((DogData) o).getKinds();
this.display();
}
}
@Override
public void display() {
System.out.println("名字:"+ this.name+"\t"+"颜色:"+this.colour+"\t"+"品种:"+this.kinds);
}
}