Jump to content

What are Observers in Java


skaterdav85

Recommended Posts

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

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

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

  • 2 months later...

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...