我正在尝试制作一个通用的观察者模式,以 headFirst 设计模式示例为蓝本。我在标有 ??? 的行出现错误 以下。
错误消息说:主题类型中的方法 registerObserver(Observer) 不适用于参数 (CurrentConditionsDisplay)
package be.intec.Meteo.Codemeteo;
import be.intec.Meteo.Interfaces.DisplayElement;
import be.intec.Meteo.Interfaces.Observer;
import be.intec.Meteo.Interfaces.Subject;
//import javax.servlet.annotation.WebServlet;
//@supressWarning("unchecked")
public class CurrentConditionsDisplay implements Observer, DisplayElement {
private float temperature;
private float humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this); // ??? Error: The method registerObserver(Observer) in the type Subject is not applicable for the arguments (CurrentConditionsDisplay)
}
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("Current conditions: " + temperature
+ "F degrees and " + humidity + "% humidity");
}
}
接口1
package be.intec.Meteo.Interfaces;
import java.util.Observer;
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
接口 2
package be.intec.Meteo.Interfaces;
public interface Observer {
public void update(float temp, float humidity, float pressure);
}
接口 3
package be.intec.Meteo.Interfaces;
public interface DisplayElement {
public void display();
}
天气数据类
package be.intec.Meteo.Codemeteo;
import java.util.ArrayList;
import be.intec.Meteo.Interfaces.Observer;
import be.intec.Meteo.Interfaces.Subject;
public class WeatherData implements Subject {
private ArrayList observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
observers = new ArrayList();
}
@Override
public void registerObserver(java.util.Observer o) {
observers.add(o);
}
@Override
public void removeObserver(java.util.Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
@Override
public void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer) observers.get(i);
observer.update(temperature, humidity, pressure);
}
}
public void mesurementChanged() {
notifyObservers();
}
public void setMeasurements(float temperature, float humidity,
float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
}
// other weather data methos here
}
测试员类
package be.intec.Meteo.Codemeteo;
import java.util.*;
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(
weatherData);
// StatisticsDisplay statisticsDisplay = new
// StatisticsDisplay(weatherData);
// ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
}