interface InterestingEvent {
// This is just a regular method so it can return something or
// take arguments if you like.
public void interestingEvent ();
}
class CallMe implements InterestingEvent {
public CallMe ()
{
// en = new EventNotifier (this);
}
// Define the actual handler for the event.
public void interestingEvent ()
{
System.out.println("Wow! Something really interesting must have occurred!");
// Do something...
}
//...
}
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 ()
{
somethingHappened = true;
// Check the predicate, which is set elsewhere.
if (somethingHappened)
{
// Signal the even by invoking the interface's method.
ie.interestingEvent ();
}
//...
}
// ...
}
public class TestCallback {
public static void main(String[] args) {
CallMe me = new CallMe();
EventNotifier event = new EventNotifier(me);
event.doWork();
}
}
Source: href="http://www.javaworld.com/javaworld/javatips/jw-javatip10.html
