1. Introduction
In many programming languages, the concept of function callbacks is present. There are function pointers in C and C++, so functions can be passed as parameters to other functions for later call. In JavaScript, function callbacks are brought to the extreme. The handling of various events, especially asynchronous events, are basically completed by function callbacks.
In Java, function callbacks can also be implemented. Although there is no function pointer, Java can obtain a class method through a reflection mechanism, pass it to other functions as a parameter of type java.lang.reflect.Method, and then call the function through the invoke method of the Method object.
Nevertheless, the calling steps in this way are relatively cumbersome, inefficient in execution, and difficult to debug. In Java, there is a mechanism that is more elegant than function callbacks, that is, interfaces.
2. Why do function callbacks need
Function callbacks are actually a way to delay the implementation of certain functions.
If we know in advance what operations the program should perform, then there is no need for function callbacks at all, and it can be implemented directly during programming.
But many times, when writing code, especially when writing tool classes, function libraries or frameworks, relatively general and abstract functions are implemented, while functions in specific scenarios are implemented by developers using these classes.
Function callbacks can solve this situation where you don’t know the specific implementation in advance.
Examples of sorting functions
For example, when we want to implement a general sorting function, we do not know in advance which types of elements other developers will use to sort, and we do not know what standards to judge the partial order (size) relationship of these elements.
Therefore, other developers can be required to provide a comparison function when using sorting functions, so that we can use compare to compare the size of the elements to be sorted without knowing in advance what type of elements is, nor the specific implementation of comparison.
Here, the compare function is a callback function for the sorting function.
The pseudo-code is represented as follows:
//The general sort function void sort(Object[] array, Method compare) { //Use the compare function to compare the size relationship of elements in array//Sort the array}//The specific comparison function is implemented by the caller int compare(Object a, Object b) { //Compare elements a and b and return the size relationship}Example of asynchronous processing functions
For example, when we write an asynchronous processing function, we do not know in advance what other developers should do when the processing is completed, because these operations can only be known when using the function in a specific scenario.
Therefore, developers can be required to provide a callback function callback when using this function. In this way, when we write asynchronous processing functions, we can call the callback function to perform some final work without knowing what these final work is in advance.
The pseudo-code is represented as follows:
//Async processing function void asynProcess(Method callback) { //Execute asynchronous task callback();}//The specific callback function void callback() is implemented by the caller { //Operations to be performed after the asynchronous processing is completed}3. Use interface instead of function callback
We mentioned above that the reason why function callback is used is because we do not know the specific implementation of certain functions in advance, so the specific implementation is left to other developers to complete.
Do you think this sentence seems to describe the Java interface? An interface is an abstract definition of a set of methods, and the specific implementation is completed by the class that implements the interface.
Therefore, using the two characteristics of object-oriented and interface-oriented, function callbacks can be replaced.
We give the two examples above to illustrate how an interface replaces a function callback.
Sort functions
Using an interface to implement the sorting function, no longer requires developers to provide the callback function compare when using the sorting function, but requires developers to ensure that the elements to be sorted implement the Comparable interface. Based on the premise that "the elements to be sorted have already implemented the Comparable interface", we can implement the sorting function without knowing the type of elements to be sorted.
//General sort function void sort(Object[] array) { //Use the Comparable interface compareTo method//Compare the size of elements to sort the array. }//Interface defined by sorting function public interface Comparable { public int compareTo(Object other);}//Implement Comparable interface public class Element implements Comparable { @Override public int compareTo(Object other) { //Judge the current size relationship between Element and other// and return the relationship between the two}}Asynchronous processing functions
When using interfaces to implement asynchronous processing functions, the developer does not require the callback function callback, but requires an object that implements the specified interface, which well reflects the Java object-oriented idea. Compared to providing a function, an object contains more information and is more flexible to use. But essentially, this asynchronous processing function still uses the interface to complete the finishing work.
//Async processing function void asynProcess(ActionListener al) { //Execute asynchronous tasks al.actionPerformed();}//Interface defined by asynchronous processing function public interface ActionListener { void actionPerformed();}//Implement ActionListener interface public class ExtraTask implements ActionListener { @Override public void actionPerformed() { //Extra work needed when the asynchronous processing function is completed}}//Calling the asynchronous processing function public static void main(String[] args) { asynProcess(new ExtraTask());}4. Summary
The callback method can be summarized as: implementing a general function func. When calling this general function in a specific scenario, the caller needs to provide a suitable callback function callback. General function func uses this callback function to complete tasks in specific scenarios.
The way to implement the interface is: implement a general function func. When calling this general function in a specific scenario, the object being operated needs to implement the appropriate interface by itself. The general function will use this interface to complete the tasks in the specific scenario.
Using function callbacks or interfaces can solve the situation where you do not know the specific implementation in advance. The function callback method passes a function, while the interface method passes an object that implements the interface.
In Java, function callbacks need to be completed using reflection mechanisms, which are prone to errors and are inefficient. Using interfaces can make the logic of the code clearer, more efficient, and easier to debug.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.