观察者模式
程序员文章站
2022-05-07 22:41:50
...
观察者模式:定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
被观察者和观察者定义了一对多的关系,观察者依赖于被观察者,被观察者对象管理某些数据,一旦数据发生改变,新的数据会以某种形式送给观察者对象。
[]类图
[]观察者模式的实现
()自定义观察者模式
被观察者接口
public interface Subject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
被观察者类
public class WeatherData implements Subject {
private List<Observer> observerList;
private Entity entity;
public WeatherData() {
this.observerList = new ArrayList<>();
}
@Override
public void registerObserver(Observer observer) {
observerList.add(observer);
}
@Override
public void removeObserver(Observer observer) {
int index = observerList.indexOf(observer);
if (index >= 0) {
observerList.remove(index);
}
}
@Override
public void notifyObservers() {
for (Observer observer : observerList) {
observer.update(entity);
}
}
public void setMeasurements(Entity entity) {
this.entity = entity;
notifyObservers();
}
}
观察者接口
public interface Observer {
void update(Entity entity);
}
观察者类
public class CurrentConditions implements Observer {
private Subject weatherData;
private Entity entity;
public CurrentConditions(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void update(Entity entity) {
this.entity = entity;
......
}
}
观察者类
public class StatisticsConditions implements Observer {
private Subject weatherData;
private Entity entity;
public StatisticsConditions(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
@Override
public void update(Entity entity) {
this.entity = entity;
......
}
}
测试
public class Text {
public static void main(String args[]) {
WeatherData weatherData = new WeatherData();
Observer mObserver = new CurrentConditions(weatherData);
Observer nObserver = new StatisticsConditions(weatherData);
weatherData.setMeasurements(new Entity(10, 20, 30));
}
() java内置观察者模式
被观察者类
public class WeatherData extends Observable {
private Entity entity;
public void setMeasurements(Entity entity) {
this.entity = entity;
setChanged();
notifyObservers(entity);
}
public Entity getEntity() {
return entity;
}
}
被观察者类继承java.util.Observable类, 提供的get….()可以用来实现观察者模式的拉数据,
调用setChanged()将changed标志设置为true,让notifyObservers()知道被调用时应该更新观察者。如果notifyObservers()前没有调用setChanged(),那么观察者不会被通知。这样的好处:可以控制何时通知观察者。
public void notifyObservers() {
this.notifyObservers((Object)null);
}
public void notifyObservers(Object var1) {
Object[] var2;
synchronized(this) {
if(!this.changed) {
return;
}
var2 = this.obs.toArray();
this.clearChanged();
}
for(int var3 = var2.length - 1; var3 >= 0; --var3) {
((Observer)var2[var3]).update(this, var1);
}
}
.......
protected synchronized void setChanged() {
this.changed = true;
}
protected synchronized void clearChanged() {
this.changed = false;
}
public synchronized boolean hasChanged() {
return this.changed;
}
观察者类
public class CurrentConditions implements Observer {
private Observable observable;
private Entity entity;
public CurrentConditions(Observable observable) {
this.observable = observable;
observable.addObserver(this);
}
@Override
public void update(Observable observable, Object o) {
if (o instanceof Entity) {
this.entity = (Entity) o;
......
}
}
}
观察者类
public class StatisticsConditions implements Observer {
private Observable observable;
private Entity entity;
public StatisticsConditions(Observable observable) {
this.observable = observable;
observable.addObserver(this);
}
@Override
public void update(Observable observable, Object o) {
if (o instanceof Entity) {
this.entity = (Entity) o;
......
}
}
}
观察者类实现java.util.Observer接口,
测试
public class InlayText {
public static void main(String args[]) {
WeatherData weatherData = new WeatherData();
Observer mObserver = new CurrentConditions(weatherData);
Observer nsObserver = new StatisticsConditions(weatherData);
weatherData.setMeasurements(new Entity(40, 50, 60));
}
}
内置的观察者模式,jdk提供的Observable是一个类而不是一个接口,由于java是单继承,导致被观察者无法继承其它的类。
[]
()使用观察者模式,可以从被观察者推或拉数据。
()有多个观察者时,不可以依赖特定的通知次序。
上一篇: (3):数据库完整性和约束_MySQL