• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About observer,observable

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I want to know in which case it's better for a class to implement observer,observable.How to use them properly?
Any help will be appreciated.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Observer and Observable are used when one object needs to "observe" or "listen to" changes in another object. The event model is an elaborate implementation of this concept. Basically, an Observable object has an array that stores references to objects impelmenting the "Observer" interface. Since those objects implement Observer, they must have a method called "update". The Observable object "tells" the listening objects that an event took place by running update on each object in the array.
Observable/Observer can be handy, and certainly illustrate an important OO issue. However, coding your own mechanism for storing references to observing objects, and broadcasting events to those objects (by running an interface-specified method on each reference) isn't too hard to code from scratch. It's also not very realistic to think your classes will be able to sub-class Observable becuase they'll already be in your class hierarchy.
An alternative to coding your own mechanism is to have a property be an Observable object. For example, assume you have class Employee with a "name" property. You might want other objects to be able to detect when the employee name changes. To do this you could have a property called "nameChangeObservable". The calling routine would implement Observer (and therefore provide an update method) and do something like:
anEmployee.getNameChangeObservable().addObserver(this);
In the class Employee setName() routine you would verify that the name is changing, then do:
this.getNameChangeObservable().notifyObservers(this);
This results in all observers having their update method run.
Doing the observer/observable thing isn't too easy in a distributed environment. And it may not be relevant. For example, in a servlet each HTTP request is a-synchonous, so there really is no way to tell each browser that a change has been made to an object they are viewing. As far as I can tell, EJB doesn't address this either.
 
reply
    Bookmark Topic Watch Topic
  • New Topic