JAVA implements callback
Developers who are familiar with the MS-Windows and X Windows event-driven design patterns usually pass a pointer to the event source and call this method when an event occurs (also known as a "callback"). Java's object-oriented model currently does not support method pointers, and it seems that this convenient mechanism cannot be used.
Java supports interface, and the same callback can be implemented through interface. The trick is to define a simple interface and declare a method that is called back by the desired one.
For example, suppose that an event is notified when it occurs, we can define an interface:
public interface InterestingEvent { // This is just an ordinary method, which can receive parameters or return the value public void interestingEvent();} In this way, we have any handle grip that implements this interface class object.
When an event occurs, the object that implements the InterestingEvent interface needs to be notified and the interestingEvent() method needs to be called.
class EventNotifier { private InterestingEvent ie; private boolean somethingHappened; public EventNotifier(InterestingEvent event) { ie = event; somethingHappened = false; }public void doWork() {
if (somethingHappened) {
// When an event occurs, notify the interface by calling this method
ie.interestingEvent();
}
}
}In this example, somethingHappened is used to sign whether an event has occurred.
The class that wishes to receive event notifications must implement the InterestingEvent interface and pass its own reference to the event notificationer.
public class CallMe implements InterestingEvent { private EventNotifier en; public CallMe() { // Create a new event notification object and pass it to it en = new EventNotifier(this); } // Implement the method of actually handling events when an event occurs public void interestingEvent() { // This event occurs, process it}}The above is a very simple example to illustrate the implementation of callbacks in Java.
Of course, multiple objects interested in this event can also be registered in the event management or event notification class.
1. Define an interface InterestingEvent, and the callback method unknownEvent(String event) simply receives a String parameter.
interface InterestingEvent { public void interestingEvent(String event);}2. Implement the InterestingEvent interface and event processing class
class CallMe implements InterestingEvent { private String name; public CallMe(String name){ this.name = name; } public void interestingEvent(String event) { System.out.println(name + ":[" +event + "] happened"); }} 3. Event manager, or event notification
class EventNotifier { private List<CallMe> callMes = new ArrayList<CallMe>(); public void regist(CallMe callMe){ callMes.add(callMe); } public void doWork(){ for(CallMe callMe: callMes) { callMe.interestingEvent("sample event"); } } }4. Test
public class CallMeTest { public static void main(String[] args) { EventNotifier ren = new EventNotifier(); CallMe a = new CallMe("CallMe A"); CallMe b = new CallMe("CallMe B"); // regiest ren.regist(a); ren.regist(b); // test ren.doWork(); }}The above is an introduction to the Java callback mechanism. Students in need can refer to it.