通用观察者 - 投射问题

时间:2013-03-14 19:25:12

标签: java compiler-errors observer-pattern subject-observer

我正在尝试制作一个通用观察者模式,模仿headFirst设计模式示例。我在标有???的行上收到错误下方。

ERROR消息说:Subject类型中的方法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);
    }
}

2 个答案:

答案 0 :(得分:3)

您的Subject界面导入了错误的Observer类。

你有:

import java.util.Observer;
public interface Subject {

你需要:

import be.intec.Meteo.Interfaces.Observer;
public interface Subject {

答案 1 :(得分:-1)

您正尝试以WeatherData.registerObserver(java.util.Observer)作为参数调用this。但是,由于be.intec.Meteo.Codemeteo.CurrentConditionsDisplay的实例仅实现be.intec.Meteo.Interfaces.Observerbe.intec.Meteo.Interfaces.DisplayElement接口,因此会导致编译错误。

有几点意见:

  • Java包应全部小写(以便更容易区分什么是类和什么是包)
  • 通常情况下,如果您的班级名称不与其他班级共享名称以避免混淆(例如be.intec.Meteo.Interfaces.Observer vs java.util.Observer
  • ,则会更容易