In computer programming, a callback function, or callback, refers to a piece of executable code that passes through a function parameter to another code. This design allows the underlying code to invoke subroutines defined at the top level.
Let's write a small example to understand the callback.
Class 1 Diagram:
The Boss class implements the Setcallback method in the interface Mycallinterface,worker to register the callback object.
2 Create a callback interface that tells the boss how to find his way through the job: Leave the boss's office address:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package com.tgb.callback;/** * This interface is the way to contact, whether it is a phone number or contact address, as * The owner must implement this interface * @author yingjie* */public Interface Mycalli nterface {public void printname ();} </span>
3 Create callback object, is the boss itself, in order to let the staff work done to tell the boss
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package com.tgb.callback;/** * The boss is a top-level application, the lower-level application (employee) is not aware of the method, so he wants to be a downlevel application (employee) Call must implement this interface * @ Author Yingjie * * * /public class Boss implements mycallinterface{@Overridepublic void Printname () { System.out.println ("This is the Boss Printname method");} } </span>
4 Create the control class, which is the employee object, he must hold the address of the Boss (callback interface)
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package Com.tgb.callback;public class Worker {private Mycallinterface callinterface; Public Worker () { } //Tell the Boss how to contact, that is, register public void Setcallback (Mycallinterface callinterface) { This.callinterface = Callinterface; } public void Callboss () { callinterface.printname (); } } </span>
5 Client Calls
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >package Com.tgb.callback;public class Client {public static void Main (string[] args) { worker worker = new Wo Rker (); Worker.setcallback (New Boss ()); Worker.callboss (); Tell the boss to finish the job } </span>
6 Why is the callback method used?
Normally a developer uses an already defined API, which is called a call. But sometimes this does not meet the requirements, it is necessary for the programmer to register their own programs, and then let the prior definition of a lot of APIs at the appropriate time to invoke the method of registration, called callbacks.
Callback is used for inter-layer collaboration, the upper layer of the function is installed on the lower level, this function is a callback, and the lower class triggers a callback under certain conditions, for example, as a driver, is a bottom, he received a data, in addition to the completion of this layer of processing work, will also be a callback to the upper application layer for further processing This is common in hierarchical data communication.
In fact, callbacks and APIs are very close, and their commonalities are functions that are called across layers. But the difference is that the API is the lower level of the call to the upper layer, generally this function is known to the upper level, and the callback is the opposite, he is the upper level to provide to the bottom of the call, for the lower layer he is unknown, must be installed by the high level, this installation function is a low-tier API, After installation the lower level does not know the name of the callback, but it uses a function pointer to save the callback, when the call is required, simply reference the function pointer and related parameter pointers.
7 Summary
The callback in Java is to obtain a reference to another object and then invoke the method in the object.
java--callback method