headFirst Design Patterns の例をモデルにした、一般的なオブザーバー パターンを作成しようとしています。??? でマークされた行でエラーが発生します。下。
エラーメッセージは次のように述べています: サブジェクト型のメソッド 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();
}
Weatherdata クラス
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);
}
}