Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

callback implementation

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to implement callback methods in java
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sen
We can see the use of callbacks in Java event model where objects registers themselves as Listeners and gets notified when the event occurs. That is a kind of Callback only I would say.
Also, you can look for Observer and Observable classes which are also callback examples.
You can refer to the site - http://www.dickbaldwin.com/java/Java077.htm
to see a possible approach for implementing callbacks.
To me callbacks can be simply implemented by the same logic the event model uses with 'registering' listeners and make observer and observable objects follow some interface so we can call callback methods.
Now, if we are coming from C background then callbacks are really simple out there. Just cast funtion pointer to unsigned int if you will and it will work.
If we want to have a callback mechanism where we wouldn't like to make the called back object follow some interface and have ability to pass the method name as string while registering the callback then we would endup using Reflection to invoke the method when the callback needs to take place...
Hope I didn't confuse you.
Regards
Maulin
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find Observer and Observable overly complicated. I wrote my own base publisher class that adds and removes subscribers and requires me to hand-code the actual messaging methods like this:

A simple call to myPublisher.myMethod() gets forwarded to all subscribers.
The publisher and subscribers both implement an interface that defines the events or methods that can occur. Note that this lets me fire any method on subscribers, pass any parameters, and even make use of return values if I want to.
My Messaging Patterns page has a section on Pub-Sub that shows a couple variations.
Hope that helps!
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic