UPDATE添加了完整的代码,以便更容易理解。
我正在尝试理解如何使用Java内置的观察者来实现推送和拉通知。
Observable类有两个方法notifyObservers()和notifyObservers(Object arg)
根据docs:每个观察者都有其带有两个参数的update方法:这个可观察的对象和arg参数。
这是我的观摩班
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Observable observable;
public CurrentConditionsDisplay(Observable observable) {
this.observable = observable;
observable.addObserver(this);
}
@Override
public void display() {
System.out.println("Current conditions: " + temperature + "F degrees and "
+ humidity + "% humidity");
}
@Override
public void update(Observable o, Object arg) {
/*
if (o instanceof WeatherData) {
WeatherData weatherData = (WeatherData) o;
this.temperature = weatherData.getTemperature();
this.humidity = weatherData.getHumidity();
}*/
if (arg instanceof WeatherData) {
WeatherData weatherData = (WeatherData) arg;
this.temperature = weatherData.getTemperature();
this.humidity = weatherData.getHumidity();
}
display();
}在我的课堂上
public class WeatherData extends Observable {
private float temperature;
private float humidity;
private float pressure;
private void measurementsChanged() {
setChanged();
//notifyObservers();
notifyObservers(this);
}
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
measurementsChanged();
}
public float getTemperature() {
return temperature;
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}我尝试过这两种方法,这两个对象都可以转换为WeatherData(观察者),然后从它们获取数据。
对我来说,使用这两种方法似乎都是推送通知类型,那么有什么区别呢?以及如何使用它实现拉通知类型?
发布于 2020-07-26 21:04:28
每个观察者都有自己的update方法,其中包含两个参数:这个可观察的对象和
null。换句话说,此方法等价于:notifyObservers(null)
这意味着您不应该调用notifyObservers(this)--这是多余的。通常,参数将是有关更改事件的附加数据。这类似于更现代的事件侦听器类(“可观察”和“观察者”现在已不再受欢迎。),这些类的事件除了事件源之外还包含数据。例如,如果将一个ActionListener添加到一个按钮中,则在运行时按下该按钮将使用一个包含数据的事件调用ActionListener的actionPerformed方法,例如该操作发生的时间。
拉通知实际上根本不是一个通知。拉扯意味着你不会等待别人告诉你事情发生了变化,你会问是否有什么事情发生了。
这方面的一个例子是在类中保留一个布尔字段,该字段指示是否发生了任何更改:
public class WeatherData {
private boolean changed;
// (other fields)
public boolean checkForChanges() {
boolean hasChanged = changed;
// Now that someone has checked, reset the change flag
changed = false;
return hasChanged;
}
private void measurementsChanged() {
changed = true;
// Nothing else to do here. Caller must "pull" (request) the
// new state, by calling checkForChanges.
}至于如何进行拉扯,只需在您的WeatherData类中保留对CurrentConditionsDisplay对象的引用,并检查它是否已更改:
public class CurrentConditionsDisplay implements DisplayElement {
private final WeatherData weatherData;
public CurrentConditionsDisplay(WeatherData observable) {
this.weatherData = observable;
}
@Override
public void display() {
// This is the pull.
if (weatherData.checkForChanges()) {
this.temperature = weatherData.getTemperature();
this.humidity = weatherData.getHumidity();
}
System.out.println("Current conditions: " + temperature + "F degrees and "
+ humidity + "% humidity");
}https://stackoverflow.com/questions/63104951
复制相似问题