Java Implementation callback
Developers familiar with the ms-windows and X Windows Event-driven design patterns typically pass a pointer to a method to an event source, calling this method (also called a "callback") when an event occurs. Java object-oriented models do not currently support method pointers and seem to be unable to use this convenient mechanism.
Java supports interface, which enables the same callback through interface. The trick is to define a simple interface, stating a method that is expected to be recalled.
For example, suppose that when an event occurs, we can define a interface:
Public interface Interestingevent {
//This is just a normal way to receive parameters or return a value of public
void Interestingevent ();
}
So we have any handle grip that implements this interface class object.
When an event occurs, you need to notify the object that implements the Interestingevent interface and call the Interestingevent () method.
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, it is notified by calling this method of the interface
Ie.interestingevent ();
}
}
In this example, use somethinghappened to mark whether an event occurred.
Classes that want to receive event notifications must implement the Interestingevent interface and pass their references to the notification of the event.
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);
}
When an implementation event occurs, the method that actually handles the event is public
void Interestingevent () {
//This event occurs and is processed
}
}
The above is a very simple example to illustrate the implementation of callbacks in Java.
Of course, you can also register multiple objects that are interested in this event in the event management or event notification class by registering them.
1. Defines an interface interestingevent, and the callback method Nterestingevent (string event) simply receives a string argument.
Interface Interestingevent {public
void Interestingevent (String event);
}
2. Implement Interestingevent interface, event handling 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 the Java callback mechanism of the introduction, the need for students can refer to.