<1> Callback is a callback function, so-called callback function, it means that the function will be registered in a certain place in the system, so that the system knows this function.
Presence, then in the future, when an event occurs, then call this function to respond to the event. Add a listener in the AWT or SWING program.
In fact, this process is a process of time callback. The specific principles are not good, you can refer to Java's Command design mode, Command model
The style is an object-oriented alternative to the callback mechanism. ------------------------- <2> A class has been completed by a predefined protocol, and other objects have passed the same agreement. After class information, this type of object can be generated until the method of the generated object is called. The process of calling is the callback.
Java's Callback is implemented by an interface. Example: 1.class a, class b2.class A Implementation Interface Operate3.class B Has a function test (OPERATE O) of the Operate Interface Type Test (Operate O) 4.Class A runtime Call Class B Test function, with its own incoming parameters 5.Class B has acquired A, you can call the method in the Operate interface implemented at any time ------------------------- <3> The callback is When you call a function, we can pass a parameter, and this parameter is a function defined in the caller for
The head is turned back in the called function to invoke a function transmitted as a parameter.
For example, C code is as follows: #include
Void fun2 (void) {printf ("asdf");}
Void fun1 (void fun2 (void) {fun2 ();
Void fun3 (void) {fun1 (fun2);
Void main () {fun3 ();
If you use Java to implement the following approach:
Interface i {void ma ();
Public class test {public static void MB (i) {i.ma ();} public static void main (String [] args) {mA (new i () {public void ma (system.out.println "ASDF");}});}} ----------------------- <4> The so-called callback is the main thread new one thread, want to let The thread performs a method of continuing to be a new thread after performing the main thread. through
Often a static method of the main thread. Specific usage can refer to the related multi-threaded programming book. ---------------------- <5> Remember the principle of responding in JAXP SAX model event is Callback, such as parsing:
Elementa, then call startElement () When you encounter Elementa>, call endElement () method ... When you encounter root>, call overElement () method ... so repeatedly calls a specific method I remember it for Callback ..
<6> Pressure Your hand to other places to other places to call to see the design mode Command mode simple code demonstration: Interface CallbackDemo {public void callbackMethod () {}} Class (Demo): CallbackDemo CD = NULL; Public Void AddCallback (CallbackDemo CD) {this.cd = cd;} PUBLIC VOID EXECUTE () {cd.callback ();} program entry: demo demo = new demo (); demo.addcallback (New CallbackDemo ) {PUBLIC VOID CALLBACKMETHOD () {CallDetail ();}}); ... public void calldetail () {system.out.println ("i'am called!");} Demo.execute (); // You Here, the Demo is called // DEMO class to call a method //, which is called is called whether it is called a callback to make one of his methods to other objects, and it is a way to pass. address
=========== <7> Implement the equivalent function of the callback function with the Java interface (reproduced from IBM)
Author: John D. Mitchell summary before Java support method pointer, Java interfaces can not provide a good way to implement callbacks. If you are accustomed to passing the event drive programming model
The function pointer called in type, you will like this skill. Familiar with the MS-Windows and X Window System event drive programming model developers who are accustomed to passing when some event occurs (ie
Function pointer of "callback"). Java's object-oriented model currently does not support method pointers, which seems to use this good mechanism.
. But we are not a little way! Java's interface support provides a mechanism for obtaining an equivalent function of a callback. Its skill is: Define a simple interface and declare in this interface
The method we want to call.
For example, suppose we want to get notifications when an event occurs. We can define an interface:
Public interface interestingEvent {// This is just a general method. So if necessary, // which can have a return value, or receive parameters. Public void interstingevent ();
This allows us to control any objects of the class that implements the interface. Therefore, we don't have to care about any external type information. With the C code
This method should be much better than the data domain of the widget when MotiF is used to hold the C function of the object pointer.
The class that issues an event signal must wait for the object of the InterestingEvent interface, and call interessingEvent () at the appropriate time.
method.
Public Class EventNotifier {Private InterestingEvent IE; Private Boolean SomethingHappend;
Public EventNotifier (InterestingEvent Event) {// Save the event object after standing. IE = Event;
// There is no event to be reported. Somethinghapped = false;
// ...
Public void doork () {// Check the predicates set elsewhere. if (SomethingHappend) {// The event signal is issued by calling this method of the interface. IE.INTERESTINGEVENT ();} // ...} // ...}
In the above example, I use the SomethinghapPpened predicate to track whether the event should be triggered. In many cases, call this method is enough to guarantee
INTERESTINGEVENT () is sent.
The code that wants to receive event notifications must implement the InterestingEvent interface and passed your own reference to the event notification program.
Public Class Callme Implements InterestingEvent {Private EventNotifier En;
Public callme () {// Create an event notification program and passes your own reference to it. En = New EventNotifier (this);
/ / Define the actual handler for the event. Public void interestingEvent () {// 噢! I must have an incident that is interested in! // Execute some operations ...}
// ...}
This is all the work you have to do. I hope this simple Java habits will make you more confident to Java. ================
<8> Finally understands the explanation in Thinking in Java, original: "Under the Callback mechanism, an object is given some information, which allows the object to call the original object at a certain point in time, more bitterness ,Ha ha.
The callback function is actually when a function is called, the address of one of his functions (this function is called a callback function) is passed to that function as a parameter. When the function is needed, use the transferred address to call functions or members, etc. in the callback function. The program of the second edition of Thimking In Java is very clear.
Interface incrementable {void inccess ();
// Very Simple To Just Implement The Interface: Class Callee1 Implements Incrementable {Private INT I = 0; Public Void Increment () {i ; System.out.Println (i);}}
Class myincrement {public void increment () {system.out.println ("other Operation");} public static void f (Myincrement MI) {mi.increment ();}}
// If your class must implement increment () in // some other way, you must use an inner class: class Callee2 extends MyIncrement {private int i = 0; private void incr () {i ; System.out.println (i );} private class Closure implements Incrementable {public void increment () {incr ();}} Incrementable getCallbackReference () {return new Closure ();}} class Caller {private Incrementable callbackReference; Caller (Incrementable cbh) {callbackReference = cbh Void Go () {callbackreference.increment ();}}
Public class callbacks {public static void main (string [] args) {callee1 c1 = new callee1 (); callee2 c2 = new callee2 (); myincrement.f (c2); Caller Caller1 = New Caller (c1); Caller Caller2 = New Caller (C2.GetCallbackReference ()); Caller1.go (); Caller1.go (); Caller2.go (); Caller2.go ();}} ///: ~
Understand one:
In this function, Closure is the so-called callback function, and the Caller constructor can receive the Closuer type, and as the Caller Caller2 = New Caller (C2.GetCallbackReference ()) in this example; Caller2.go (); is a common call callback The function of the function, Caller2 is substantially called is the CLOSUER's increment () method! In Java, the callback function is usually used in event processing!
Understand 2: It is not that two parent classes have the same method for increment (), and Callee2 should inherit two increment (), you must inherit one of the internal classes, return to internal class objects via getCallbackReference ()
Understand three:
Myincrement is like Cao Cao, with void increment () this stuff, but not the stuff Callee2 in the Incrementable (Emperor) is Cao Yu, inherits his father's void increment (), when he wants to be an incrementable At this time, this fake void increment () is not ok, so he can only use an internal class to incrementable, in the internal class, INCREMENT () is true