观察者模式(发布订阅模式):在对象之间定义一对多的依赖,这样一来,当一个对象改变状态,依赖他的对象都会收到通知,并自动更新。
使用场景:当一个对象状态改变同时需要改变其他对象,且不知道多少观察者受影响。
观察者模式包含以下几个主要部分:
- 抽象主题接口:该主题接口主要包含的功能有注册/删除观察者、通知所有观察者。
- 具体主题类:实现抽象的主题接口,将所有属于它的观察者保存在一个集合中,每个主题可以拥有任意数量的观察者,当具体主题类内部状态发生改变时,将通知所有登记过的观察者。
- 抽象观察者接口:为所有观察者定义一个通用的接口,当收到主题的通知时将更新自己。
- 具体观察者类:存储与主题状态自恰的状态,实现抽象观察者接口所需的更新接口,以便自身与主题类的状态相协调,同时可以定义一个关于主题接口,指向自己订阅具体主题对象的引用,方便自己注销该主题。
观察者模式中,为分为拉模型和推模型。
- 推模型:主题对象向观察者推送主题的信息,不管观察者是否需要,推送的信息包含:部分或全部信息
public interface DisplayElement {
public void display();
}
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
public interface Observer {
public void update(float temperate,float humidity,float pressure);
}
public class WeatherData implements Subject {
private ArrayList<Observer> observers;
private float temperate;
private float humidity;
private float pressure;
public WeatherData(){
observers = new ArrayList<Observer>();
}
@Override
public void registerObserver(Observer o) {
// TODO Auto-generated method stub
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
// TODO Auto-generated method stub
int i = observers.indexOf(o);
if(i >= 0)
{
observers.remove(i);
}
}
@Override
public void notifyObservers() {
// TODO Auto-generated method stub
for (int i = 0; i < observers.size(); i++) {
Observer observer =observers.get(i);
observer.update(temperate, humidity, pressure);
}
}
public void measurementsChanged(){
notifyObservers();
}
public void setMeasurements(float temperate,float humidity,float pressure){
this.temperate = temperate;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
}
public class CurrentConditionDisplay2 implements Observer,DisplayElement{
private float temperate;
private float humidity;
private Observable observable;
public CurrentConditionDisplay2(Observable weatherData) {
this.observable = weatherData;
this.observable.addObserver(this);
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println("Current conditions:"+temperate
+" F degrees and "+humidity+" % humidity");
}
@Override
public void update(Observable o, Object arg) {
// TODO Auto-generated method stub
if (o instanceof WeatherData2) {
WeatherData2 weatherData2 = (WeatherData2) o;
this.humidity = weatherData2.getHumidity();
this.temperate = weatherData2.getTemperate();
display();
}
}
}
- 拉模型:主题对象在通知观察者时,只传递少量信息。由观察者主动获取信息,把主题对象通过update()传递给观察者,观察者根据自己的需求,通过该引用获取数据。
public class WeatherData2 extends Observable {
private float temperate;
private float humidity;
private float pressure;
public WeatherData2(){
}
public void measurementsChanged(){
setChanged();
notifyObservers();
}
public void setMeasurements(float temperate,float humidity,float pressure){
this.temperate = temperate;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
public float getTemperate() {
return temperate;
}
public void setTemperate(float temperate) {
this.temperate = temperate;
}
public float getHumidity() {
return humidity;
}
public void setHumidity(float humidity) {
this.humidity = humidity;
}
public float getPressure() {
return pressure;
}
public void setPressure(float pressure) {
this.pressure = pressure;
}
}
public class CurrentConditionDisplay2 implements Observer,DisplayElement{
private float temperate;
private float humidity;
private Observable observable;
public CurrentConditionDisplay2(Observable weatherData) {
this.observable = weatherData;
this.observable.addObserver(this);
}
@Override
public void display() {
// TODO Auto-generated method stub
System.out.println("Current conditions:"+temperate
+" F degrees and "+humidity+" % humidity");
}
@Override
public void update(Observable o, Object arg) {
// TODO Auto-generated method stub
if (o instanceof WeatherData2) {
WeatherData2 weatherData2 = (WeatherData2) o;
this.humidity = weatherData2.getHumidity();
this.temperate = weatherData2.getTemperate();
display();
}
}
}
以上两个例子中,推模式是用自定义接口,拉模式使用java内置观察者模式,拉/推模式都可以用自定义接口或java内置实现的。