skaterdav85 Posted April 6, 2010 Share Posted April 6, 2010 Can someone explain to me what are observers used for in Java? I'm confused on the whole idea of using listeners and observers in Java since most of my experience of application interactivity is on the web with JavaScript. Link to comment Share on other sites More sharing options...
Synook Posted April 7, 2010 Share Posted April 7, 2010 It's part of the event handling system. http://en.wikipedia.org/wiki/Observer_pattern Link to comment Share on other sites More sharing options...
skaterdav85 Posted April 7, 2010 Author Share Posted April 7, 2010 I was trying to understand the example but i dont see what the difference between an observer and a listener is. Maybe you can compare it to JS even though I know these terms don't apply. In JS, it seems like a 'listener' would be adding an event handler to some html object, so these would be comparable: JSonClick="someFunction();"JavaaddWindowListener(new LocalWindowListener()); What would the 'observer' be? Link to comment Share on other sites More sharing options...
Synook Posted April 7, 2010 Share Posted April 7, 2010 JS's object-oriented model isn't that great, so there is no real comparison. Basically, an observer watches another object for something to happen (doesn't have to be user-driven), and then gets notified when that thing happens. They are a bit like "listeners" in JS - but can you think of what that would be in Java? Link to comment Share on other sites More sharing options...
skaterdav85 Posted April 7, 2010 Author Share Posted April 7, 2010 So would an observer be added to some instance variable or object and then the update method of the observer class would be called when say the value of the instance variable or the object changes? Link to comment Share on other sites More sharing options...
Chathuranga Posted June 19, 2010 Share Posted June 19, 2010 Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.http://www.globalguideline.com/interview_q...ava_Programming Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.