Hi all.

I am up to the final stage of my system, and it requires use of the observer/observable pattern, something I haven't a clue about!

Basically, I now have a functioning "WatchList" system with which to store specific objects (Locations). Now, these objects have a number of fields relating to "stats" about that object, like "Current Weather Overview" and "Current Temperature", as well as other weathery things.

Basically, I want the values of these fields to appear in TextFields (or similar) my GUI whenever it's Location objects gets selected in my WatchList. Sounds simple enough, I think I could do that.

However, I'm thinking that perhaps I need to use the Observer pattern to update the statistics dynamically and have them change automatically when displayed, rather than having to click some sort of "refresh" button.

Anyway, I hope that makes sense. Can anybody offer any suggestions? Thanks.

Yes, Observer is the way to go. Java API uses it all the time - just look at all the "Listener" interfaces and methods in Swing for a start. You'll find loads of details and samples on the web.

Hey guys. Thanks for your replies. I've been playing around with the Observer/Observable stuff but I haven't had any luck that. Can I just run this past you...

I have my GUI as the Observer and the Location objects as Observable. Whenever a user retrieves their watchlist, they subscribe to all of their Locations using addObserver.

This is my first question, how do I make addObserver add the object calling it as the observer? Would I just do addObserver(this)?
Thanks!

The Observable object has a Collection (eg ArrayList) of Observers, so the addObserver method just adds the Observer (passed as the parameter) to the Collection. so:
Observer: observableObject.addObserver(this);
Observable:
public void addObserver(Observer o) {
observers.add(o);
}

This article has been dead for over six months. Start a new discussion instead.