Search code examples
javacompiler-errorsobserver-patternsubject-observer

Generic Observer - Casting problems


I'm trying to make a generic observer pattern, modeled after a headFirst Design Patterns example. I get an error at the line marked with ??? below.

ERROR message says : The method registerObserver(Observer) in the type Subject is not applicable for the arguments (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");
    }
}

Interface 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();
}

Interface 2

package be.intec.Meteo.Interfaces;

public interface Observer {
    public void update(float temp, float humidity, float pressure);
}

Interface 3

package be.intec.Meteo.Interfaces;

public interface DisplayElement {
    public void display();
}

Weatherdata class

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
}

Tester class

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);
    }
}

Solution

  • Your Subject interface is importing the wrong Observer class.

    You have:

    import java.util.Observer;
    public interface Subject {
    

    You need:

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