-1

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);
    }
}
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
AchillesVan
  • 4,156
  • 3
  • 35
  • 47

2 Answers2

3

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 {
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • It is a very poor choice to name a class with the same name as a class from the JDK, for exactly this reason: your IDE will tend to automatically import the JDK class, as will the brains of any readers of your code. Pick a another name! – Bohemian Mar 14 '13 at 19:35
  • Thank you. My mystake. I think i should get some rest now. Thank you again. – AchillesVan Mar 14 '13 at 19:38
  • @Bohemian I disagree and there are many cases where frameworks make use of the more common names (e.g. File). When there is a conflict, both Eclipse and Netbeans ask the developer to pick the right class to import. – Tim Bender Mar 14 '13 at 19:40
  • Yes but so often the programmer picks the wrong one by mistake and you get exactly this problem - and lots of wasted time – Bohemian Mar 14 '13 at 22:41
-1

You are trying to call WeatherData.registerObserver(java.util.Observer) withthis as a parameter. But as an instance of be.intec.Meteo.Codemeteo.CurrentConditionsDisplay only implements the be.intec.Meteo.Interfaces.Observer and be.intec.Meteo.Interfaces.DisplayElement interfaces, that causes the compile error.

A couple of observations:

  • Java packages should be all lowercase (makes it easier to distinguish as to what's a class and what's a package)
  • Usually it's easier if your class names don't share the name with other classes to avoid confusion such as this (be.intec.Meteo.Interfaces.Observer vs java.util.Observer
beny23
  • 34,390
  • 5
  • 82
  • 85
  • -1 because I think the second bullet point is terrible advice. You can't use pretty much any framework out there without having class naming conflicts. The real problem is just inexperience/carelessness while choosing imports. – Tim Bender Mar 14 '13 at 19:37
  • I'm not saying never share any class names, but I wouldn't choose class names shared with JRE classes as it's may well lead to confusion and in my view reduces the maintainability. Especially as the `java.util.Observer` class is used in the code as well. – beny23 Mar 14 '13 at 19:57