Search code examples
javaobserver-pattern

Observer pattern issues Java


I am trying to implement a simple observer pattern in java and I am running into an issue that I cannot seem to figure out. I have the following:

public interface Observable {
    public void addView(Observer view);
    public void removeView(Observer view);
    public void notifyView();
    public Object getUpdate(Observer view);
}

public class Data implements Observable {

    //List to hold all of the views observing data changes. 
    private ArrayList<Observer> views = new ArrayList<Observer>(); 
    private ArrayList<Integer> data = new ArrayList<Integer>(); 

    ...
    @Override
    public void notifyView() {
        for (Observer view: views){
             //issue here
             view.update(data);
        }

    }
    ...
}

I am getting an error in my notifyView() method when I try to call the view.update() method. The error I get is:

The method update(Observable, Object) in the type Observer is not applicable for the arguments (ArrayList<Integer>)

I do not understand this because I have my Observer interface, and class set up as follows:

public interface Observer {

    public void update(ArrayList<Integer> data); 

}

public class View implements Observer{

    @Override
    public void update(ArrayList<Integer> data) {
        // TODO Auto-generated method stub

    }

}

I have tried reading other posts but nothing has helped me. Is there something I am overlooking that is causing this problem?


Solution

  • Your code is not using your Observer class, instead, is using the Java's Observer class

    https://docs.oracle.com/javase/7/docs/api/java/util/Observer.html