Content Introduction:
- Understanding of Interface Callback and Issues Needing Attention
- General usage of interface callbacks
- Concise Use of Interface Callback
-
The Significance of Interface Callback
Introduction to interface callback:
Simply put, the interface callback is that the caller class A accesses the M method in the callee class B. This M method calls the method in class A after execution.
Question?
How does the M method in Class B access the method in Class A? If you understand this problem, you will understand the interface callback.
Let's write code based on a scenario description:
We use download file method DownLoadFile() in caller Caller class. Notify Caller class file loading when the download is complete. Caller class can access the resources just downloaded. Because download takes time. Caller class has other things to do. It can't wait until the download is complete. Do it. In that case, the download method must open a new thread to download.
General usage of interface callbacks
Code implementation:
1. Interface and callback methods:
/* CallBackListener (Callback listener interface) Interface for callbacks (abstract methods within the interface are used to listen for results returned by the callee) There are callbacks in the interface: the callback method is the method that the callee accesses the caller through the callback interface object. So the parameter should be the data type and the number of parameters that the callee returns to the caller's result. */ public interface CallBackListener { //Send a message to the caller public abstract void sendMessage2Caller(String msg); }
2. Caller class:
/* Caller :(Caller) Before accessing the method of the callee, the callback interface object must be set to the callee to facilitate the callee to access himself using the interface callback object. */ public class Caller { public static void main(String[] args) { //1. Create the callee object Callee callee = new Callee(); //2. Create callback interface objects. CallBackListener cb = new CallBackListener() { @Override public void sendMessage2Caller(String msg) { //Print the result of the callback System.out.println(msg); } }; //3. The callback interface object must be set to the callee callee.setCallBackListener(cb); //4. Accessing the download file method of the callee. When this method is completed, the callee will notify us by calling back the interface object. callee.DownLoadFile(); System.out.println("You download slowly,I'm going to do something else first....."); } }
3. Callee class
/* Callee(Callee: 1.Variables of the callback interface type must exist in the callee class. 2.There is also a setXxx() method for assigning values to the variable. 3.When the result can be returned to the caller, call the method of the interface with the variable of the interface type to access the caller. (Because the method to be executed by the variable of the interface type exists in the caller.) */ public class Callee { //1. Variables of callback interface type must exist in the callee class. private CallBackListener cb; //2. There is also a setXxx() method for assigning values to the variable. public void setCallBackListener(CallBackListener cb){ this.cb = cb; } /** * Downloading files is a very time-consuming task, and the caller needs to read the downloaded files after the download is completed. * So when this method is downloaded, the caller should be notified immediately. The action of this notification is called callback. * @throws InterruptedException */ public void DownLoadFile(){ //Open a new thread to download. Asynchronous task. new Thread(){ public void run() { long start = System.currentTimeMillis(); //Sleep simulation is time-consuming. try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); //Notify the caller when the download is complete if(cb != null){ cb.sendMessage2Caller("Download completed,time consuming:"+(end-start)/1000+"second,You can visit resources now.."); } }; }.start(); } }
The results are as follows:
Five seconds later
Concise Use of Interface Callback
Code implementation:
1. Interface and callback methods:
/* CallBackListener (Callback listener interface) Interface for callbacks (abstract methods within the interface are used to listen for results returned by the callee) There are callbacks in the interface: the callback method is the method that the callee accesses the caller through the callback interface object. So the parameter should be the data type and the number of parameters that the callee returns to the caller's result. */ public interface CallBackListener { //Send a message to the caller public abstract void sendMessage2Caller(String msg); }
2. Caller class:
public class Caller { public static void main(String[] args) { //1. Create the callee object Callee callee = new Callee(); //2. Accessing the download file method of the callee. When this method is completed, the callee will notify us by calling back the interface object. //Use anonymous inner class objects as parameters to pass. More concise callee.DownLoadFile(new CallBackListener() { @Override public void sendMessage2Caller(String msg) { //Print the result of the callback System.out.println(msg); } }); System.out.println("You download slowly,I'm going to do something else first....."); } }
3. Callee class
/** * Downloading files is a very time-consuming task, and the caller needs to read the downloaded files after the download is completed. * So when this method is downloaded, the caller should be notified immediately. The action of this notification is called callback. * final CallBackListener cb Anonymous internal classes must add final to access local variables. */ public class Callee { public void DownLoadFile(final CallBackListener cb){ //Open a new thread to download. Asynchronous task. new Thread(){ public void run() { long start = System.currentTimeMillis(); //Sleep simulation is time-consuming. try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); //Notify the caller when the download is complete if(cb != null){ cb.sendMessage2Caller("Download completed,time consuming:"+(end-start)/1000+"second,You can visit resources now.."); } }; }.start(); } }
Meaning of interface callback:
Through the above two cases, you will have a question. Why do we have an interface? Can we change the interface into a common class?
Certainly. The reason why interface callbacks are used here is that interfaces are normative and flexible, especially normative.
Code examples:
1. The original interface is replaced by abstract class and the other two classes remain unchanged, which will not affect the operation effect.
public abstract class CallBackListener { //Send a message to the caller public abstract void sendMessage2Caller(String msg); }
2. Replacing the original interface with the other two classes will not affect the operation effect.
public class CallBackListener { //Send a message to the caller public void sendMessage2Caller(String msg){ }; }