Java's support of interfaces provides a mechanism by which we can get the equivalent of callbacks. The trick is to define a simple interface that declares the method we wish to be invoked.
For example, suppose we want to be notified when an event happens. We can define an interface:
public interface InterestingEvent
{
// This is just a regular method so it can return something or
// take arguments if you like.
public void interestingEvent ();
}
This gives us a grip on any objects of classes that implement the interface. So, we need not concern ourselves with any other extraneous type information. This is much nicer than hacking trampoline C functions that use the data field of widgets to hold an object pointer when using C++ code with Motif.
The class that will signal the event needs to expect objects that implement the InterestingEvent interface and then invoke the interestingEvent() method as appropriate.
public class EventNotifier
{
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier (InterestingEvent event)
{
// Save the event object for later use.
ie = event;
// Nothing to report yet.
somethingHappened = false;
}
//...
public void doWork ()
{
// Check the predicate, which is set elsewhere.
if (somethingHappened)
{
// Signal the even by invoking the interface's method.
ie.interestingEvent ();
}
//...
}
// ...
}
In that example, I used the somethingHappened predicate to track whether or not the event should be triggered. In many instances, the very fact that the method was called is enough to warrant signaling the interestingEvent().
The code that wishes to receive the event notification must implement the InterestingEvent interface and just pass a reference to itself to the event notifier.
public class CallMe implements InterestingEvent
{
private EventNotifier en;
public CallMe ()
{
// Create the event notifier and pass ourself to it.
en = new EventNotifier (this);
}
// Define the actual handler for the event.
public void interestingEvent ()
{
// Wow! Something really interesting must have occurred!
// Do something...
}
//...
}
That's all there is to it. I hope use this simple Java idiom will make your transition to Java a bit less jittery.
Worst explanationBy Anonymous on June 26, 2009, 3:55 pmWorst explanation I've read, and (perhap because) this is the first I read about, the example is very confussing, and have no main to test.
Reply | Read entire comment
Thank you - my first use of interfaces.By Anonymous on May 29, 2009, 4:22 pmThank you - my first use of interfaces.
Reply | Read entire comment
Cannot instantiate the typeBy Anonymous on March 22, 2009, 3:02 amI tried following the example, but I get the error "Cannot instantiate the type InterestingEvent"
Reply | Read entire comment
Thanks for the tip. By Athen on March 11, 2009, 5:24 pmThanks for the tip.
Reply | Read entire comment
Someone appears to have copied this articleBy Anonymous on March 11, 2009, 2:59 pmhttp://www.builderau.com.au/program/java/soa/Implementing-the-callback-pattern-in-Java/0,339024620,339289702,00.htm
Reply | Read entire comment
View all comments