• 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

what is Observable class and Observer interface

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are these Observable class and Observer interface?
what they are for why these clasess are introduced by java :roll: ?
what is the actual use?
is this classes are really important in building a webapplication?

till now i havn't seen any example for this
can any one provide me an example for this program?

thanks to all

regards
cinux
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a design pattern called Observer. It is useful to notify several objects (Observer) when something has happened to one instance (Observable).

It is commonly used in Ajax to notify several objects when an event (like onclick) occurs.
What you basically do is :
1. Have an Observable class
2. Have a few Observer classes
3. Register the Observers to the Observable object (addObserver)
4. Call notifyObserver when you want to notify all the observers that something has happened
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've made a little sample. I forgot to say that setChanged has to be called for all observers to be notified.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like the example

Observer/Observable are about the simplest event distribution method you can use that decouples source (Obervable) and listener (Observer); there's nothing about it that is particularly applicable to web applications. Its drawback is that Observer must be extended, which may or may not be acceptable in a particular situation. But since there isn't much to the Observer class (look at its source code and you'll see what I mean), you can just as easily incorporate its functionality into your own class. (Note that you can pass additional information to the Observer through the second parameter of the update method.)

I've used this mechanism a few times in MVC scenarios (with Observable being the model and Observer being the controller), but these days I'd prefer custom event classes with an event register/unregister mechanism.
[ August 29, 2006: Message edited by: Ulf Dittmer ]
 
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic