Explanation of the difference between Java callback function principle example and proxy mode

  • 2021-06-28 12:43:41
  • OfStack

Examples of java callback functions, their principles, and differences from proxy mode

callback

Applications (application program) often invoke functions prepared in the library through API.However, some library functions (library function) require the application to pass it a function first, which is called when appropriate, to accomplish the target task.This function that is passed in and then called is called a callback function (callback function).

Role: You can separate the caller from the callee.The caller does not care who is called. All it needs to know is that there is a called function with a certain prototype and some restrictions, such as an int return value.

Example

A tool class for calculating the run time of a function, normal method


/**
 * @Auther: cpb
 * @Date: 2019/1/17 16:32
 * @Description:
 */
public class RunningTime {
  /**
   * 1 Of the methods to be tested 1 More time-consuming cycles 
   */
  public  static  void testMethod(){
    for ( int i= 0 ; i< 100000000 ; i++){
    }
  }
  /**
   * 1 A simple way to test method execution time 
   */
  public void testTime(){
    long begin = System.currentTimeMillis(); // Test Start Time 
    testMethod(); // test method 
    long end = System.currentTimeMillis(); // Test End Time 
    System.out.println("[use time]:" + (end - begin)); // Print Usage Time 
  }
  public static void main(String[] args) {
    RunningTime test=new RunningTime();
    test.testTime();
  }
}

Create callback function interface


public interface CallBack {
  // Method of performing callback operation 
  void execute();
}

callback


/**
 * @Auther: cpb
 * @Date: 2019/1/17 16:35
 * @Description:
 */
public class Tools {
  /**
   *  Test function usage time by definition CallBack Interface execute Method 
   * @param callBack
   */
  public  void testTime(CallBack callBack) {
    long begin = System.currentTimeMillis(); // Test Start Time 
    callBack.execute(); /// Callback operation 
    long end = System.currentTimeMillis(); // Test End Time 
    System.out.println("[use time]:" + (end - begin)); // Print Usage Time 
  }
  public  static  void main(String[] args) {
    Tools tool = new Tools();
    tool.testTime(new CallBack(){
      // Definition execute Method 
      public  void execute(){
        // You can add it here 1 More than one method to test run time 
        RunningTime.testMethod();
      }
    });
  }
}

Why Callback Function

Let's watch a process running the program, tools- > CallBacks- > tools, considered a callback function.

proxy pattern


// Interface 
public interface CallBack {
  // Method of performing callback operation 
  void execute();
}
// Implementation Class 
public class MyCallBack implements CallBack{
  @Override
  public void execute() {
    MyCallBack.testMethod();
  }
  public  static  void testMethod(){
    for ( int i= 0 ; i< 100000000 ; i++){
    }
  }
}
// proxy class 
public class Proxy implements CallBack {
  private CallBack callBack;
  public Proxy(CallBack callBack){
    this.callBack = callBack;
  }
  @Override
  public void execute() {
    callBack.execute();
  }
}
// Call Method Class 
public class Tools {
  /**
   *  Test function usage time by definition CallBack Interface execute Method 
   * @param callBack
   */
  public  void testTime(CallBack callBack) {
    long begin = System.currentTimeMillis(); // Test Start Time 
    callBack.execute(); /// Callback operation 
    long end = System.currentTimeMillis(); // Test End Time 
    System.out.println("[use time]:" + (end - begin)); // Print Usage Time 
  }
  public  static  void main(String[] args) {
    Tools tool = new Tools();
    // Let the proxy implement the method to invoke the method 
    Proxy proxy = new Proxy(new MyCallBack());
    tool.testTime(proxy);
  }
}

The difference between proxy mode and callback function

Proxy mode requires creating interface implementation classes and placing them in proxy classes for better isolation and scalability Callback functions are easy to write without creating interface implementation classes.

summary


Related articles: