• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Umm, how does Observer work?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?!?!?!");
}

}
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you calling it???
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Blanco:
it still ... notifies an observer.



You're right -- sorry for the oversight.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic