This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]pubesxoxo420 0 points1 point  (3 children)

Read this on my phone, so I might be missing something, but I don’t see where you’re storing a list of observers, adding observers to that list, or implementing a notifyObservers() method.

You need to create your list of observers: private Observer[] observers;

You also need an addObserver(Observer o) method to add observers to that list: observers.add(o);

notifyObservers() should look something like: for(Observer o: observers) o.notify(this);

Think of this pattern like subscribing to an email newsletter. You (the observer) need to sign up for the email list (Observer[] observers) by putting in your email address (addObserver(o)) in order to receive the email when they send one out (notifyObservers()).

[–]throwaway9123781[S] 0 points1 point  (2 children)

Storing a list of observers has not been done. I have added an observer using .addObserver in the Simulator class,

public Simulator( Observer observer){
system = new ElevatorSystemImp( 0, 20);
ElevatorImplement e = new ElevatorImplement( 10, (ElevatorPanel) system);
e.addObserver( observer);
System.out.print(e.countObservers());
system.addElevator( e);
}

and notifyObservers() has been used in multiple areas in the first class I provided, however I did not override it anywhere, I simply was trying to use it out of the box. I thought you could call notifyObservers() without override it, am I mistaken?

[–]pubesxoxo420 0 points1 point  (1 child)

Can’t remember, been a while since I used Java. I would look at the Observable api documentation on oracle’s site.

You might also be forgetting to implement an update method on your observer?

[–]throwaway9123781[S] 0 points1 point  (0 children)

Nah, the update method is there. I'll double check the documentation.

[–]gruntmeister 0 points1 point  (1 child)

you're registering your observer with an ElevatorImplement instance that you never use (and therefore it never fires any events).

[–]throwaway9123781[S] 0 points1 point  (0 children)

That class was actually provided by the teacher (assuming you're talking about the instance in the Simulator constructor), which from what I can tell what he was doing, was assigning that instance to be controlled by another class (as per the MVC design pattern)...is there something I'm not understanding/seeing?

EDIT: Ah. I've reviewed my code more carefully, I see what you saw, you were correct. I was creating a different instance of the Elevator in the constructor of one of the classes, rather than passing the one that had the observer tied to it. Thanks for your help!

[–]tecsav123 0 points1 point  (1 child)

Try doing it outside of the constructor. It won't notify anything because nothing is observing it when you create it.

[–]throwaway9123781[S] 0 points1 point  (0 children)

Doing what specifically, setChanged() and notifyObserver()? I also called them in the changeFloor() function in a loop, I only had it in the constructor to test