1. What is a callback function
Callback functions (callback function), as the name implies, functions for callbacks. A callback function is just a functional fragment, a function implemented by the user in accordance with the callback function calling convention. A callback function is a part of a workflow that determines the timing of the call (callback) of a function by a workflow. The callback function contains the following features:
1, part of the workflow;
2, must be declared (defined) according to the calling convention specified by the workflow,
3, The timing of his invocation is determined by the workflow, and the implementation of the callback function cannot directly invoke the callback function to implement the function of the workflow;
2. callback mechanism
callback mechanism is a common design model, He exposes a function within the workflow to external users in accordance with the agreed interface, providing data to external users, or requiring external users to provide data.
=============================== ========================
Java Callback mechanism:
There is always a certain interface between software modules, which can be divided into three categories: synchronous invocation, callback and asynchronous invocation.
Synchronous invocation: A kind of blocking call, the caller waits for the other party to complete before returning, it is a one-way invocation;
Callback: A two-way invocation pattern, that is, the callee will also invoke the interface when the interface is called;
Asynchronous invocation: A mechanism similar to a message or event, but it is called in the opposite direction, and the service of the interface proactively notifies the client (that is, the interface that invokes the client) when a message is received or an event occurs.
Callbacks are very closely related to asynchronous calls: Use callbacks to register asynchronous messages, and implement notification of messages through asynchronous calls.
========================================================
In Java, for example:
1 Packagecallbackexample;2 PublicInterfaceIcallback3 {4//methods that require callbacks5 Public voidpostexec (); 6 } 7 8 //another class:9 PackageCallbackexample;
PublicclassFooBarTen { One //principle of Combinatorial aggregation A PrivateIcallback CallBack; - PublicvoidSetcallback (icallback callBack) - { the This. CallBack =CallBack; - dosth (); - } - Publicvoiddosth () + { - callback.postexec (); + } A}
The second class, inside the test class, is an anonymous class:
1 Packagecallbackexample;2 PublicclassTest3 {4 PublicStatic voidMain (string[] args)5 {6FooBar foo =NewFooBar ();7Foo.setcallback (NewIcallback ()8 {9 Public voidpostexec ()Ten { OneSystem.out.println ("a reference to an object implemented in the test class that cannot be test, but called by the Foobar object"); A } - } - ); the } -}
Code of Appeal:
1. Two classes: Anonymous class and Foobar
2. Anonymous class implements interface Icallback (implemented in the form of an anonymous class in the main method of test testing) 3.FooBar has a function setcallback (Icallback O) 4 with a parameter of Icallback interface type. The anonymous class runtime calls the Setcallback function in Foobar to pass in the parameter itself
5.FooBar has obtained an anonymous class, you can callback the method in the Icallback interface implemented in the anonymous class at any time
==================================
1. The concept of first callback method is not the same as the concept of "construction method", it does not refer to a method in Java that has special meaning or purpose. 2. Call it a "callback" of the method is more appropriate, it refers to the method of a call way. Any method that is called "callback" can be called a "callback method"
3. The callback for a method usually occurs during the use of the Java interface and abstract classes. Assume that there is an interface named Icallback which has the method named Postexec ()
There is a class MyClass implementation of the interface, that is, the implementation of the Postexec () method. Now there is another class Foobar it has a method Setcallback (Icallback callBack), and the Setcallback method calls CallBack () method.
If we now use an instance of MyClass MyClass, take it as a parameter into the
Setcallback (Icallback callBack) method, we say that the Setcallback (Icallback CallBack) method callbacks the MyClass () method.
The following is a tool class that uses a Java callback function to test the run time of a function: if we want to test the execution time of a method of a class, we usually do this:
Java callback mechanism