Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Umm, how does Observer work?

 
John Blanco
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel like such a dunce. What's wrong with this code? The update() never gets called?

import java.util.*;

public class Whatever implements Observer {
public static void main(String[] args) {
new Whatever();
}

public Whatever() {
Observable obj = new Observable();

obj.addObserver(this);
obj.notifyObservers();
}

public void update (Observable o, Object obj) {
System.out.println("Why isn't this called?!?!?!");
}

}
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you calling it???
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
update() will be called by the Observable if someone ever calls the notifyObservers() method. Since the Observable is local to your Observer's constructor, there's no way this will ever happen.

Normally the way these things are used is that you make a subclass of Observable, Observers register with it, and then the subclass calls notifyObservers() on itself when "something happens" -- i.e., when there is an event that it wants the Observers to know about.
 
John Blanco
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example is contrived...in actual practice, it's useful. I don't see why this code, however, doesn't work? Yes, the Observable is local, but it still registers and then notifies an observer.

It should work.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the example is convoluted, but notifyObservers() is indeed called. The real problem is that setChanged() has never been called. The API is not very clear about this, but when notifyObservers() is called, the first thing it does is check whether the object has been "changed" (meaning, has setChanged() been called) and if not, it does nothing. Here's an example that works:
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should also be noted that java.util.Observer and Observable aren't God's
gift to OOP. (For example, on either's javadoc page, go to the top and click
on the USE link. They aren't used in the rest of the Java API!) There's not a
lot of code in Observable, so if you want to code the Observer pattern from
scratch or using, say, SwingPropertyChangeSupport, go for it.
[ November 22, 2005: Message edited by: Jeff Albrechtsen ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Blanco:
it still ... notifies an observer.


You're right -- sorry for the oversight.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic