Let's talk more about Java callback functions

  • 2020-04-01 04:05:32
  • OfStack

I encountered the callback function again, and I'm going to write it down and share it this time.

Callbacks, or callbacks as they are called in object-oriented languages, are simply functions that are called back at a certain time.

More specifically, A function A is passed in as an argument to another function B, which is then called by B at some time.

The question here is, why pass a function as an argument to another function when it's calling another function and you can call it in the body? And some languages, such as Java, don't support functions as arguments.

Right, can call another function in the body of the function, the function to be no difference, but there is A problem, is that you want to call this function is written is dead, that is to say, this function can call A function A B, so that if in another scenario, A function with A different implementation C also need at some point B is called, what to do with it.

Let's move on to callbacks. In c/c++, callbacks can be called by another function using a function pointer as an argument. In c#, you can use the delegate, if it's an event method, and the event keyword; In python and javascript, you can directly pass functions as object arguments, and these languages are very good at implementing callback functions (methods), but Java? First said as an aside, the point since learned c #, don't like Java, once after going to no longer use Java, but the reality is not so ideal, I have to do now android, so still can't let go of Java, and met the callback function problem, today also is met from the Java, personally, I think in the language appears in the blog, in addition to Java, for correction, can be easy, and better understand the implementation of Java, however, I think that is not so, otherwise I will not to write this blog.

Okay, moving on to the implementation of callback methods in Java. The point of this blog is Java. In Java, callback method is implemented by borrowing the interface. I found a sentence on the Internet:

Assign a reference to an object created by a class that implements an interface to an interface variable declared by that interface, and that interface variable can call the methods of the interface being implemented.
It's a little confusing. Just a quick explanation:
There is an interface with a method in it (which is the method to call back):


interface CallBackInterface {
  void callBackMethod();
}

We know that an interface object cannot be used directly because none of the methods are implemented. So find a class that implements this interface.
So now add a class to implement the interface:


interface CallBackInterface {
  void callBackMethod();
}

class CallBackClass implements CallBackInterface{

  @Override
  public void callBackMethod() {
    System.out.println("hello");
  }
}

Okay, last step: assign the object of the class that implements the interface to the declared interface variable (I wrote it into a method and then put a class shell around it):


public class CallBackTest {

  interface CallBackInterface {
    void callBackMethod();
  }

  class CallBackClass implements CallBackInterface {

    @Override
    public void callBackMethod() {
      System.out.println("hello");
    }
  }

  public void showCallBack() {
    CallBackInterface itfs = new CallBackClass();
    itfs.callBackMethod();
  }
}

Now you can try it:


public class Test {
  public static void main(String[] args) {
    new CallBackTest().showCallBack();
  }
}

If it's not a surprise, I'm going to print hello, but I am.

I'm done with the examples, so what did I do? Detailed points again, saying, we have a want to be in a certain way the called method, this method is a callback method), in front of us said it is best not to direct to the callback method do write directly in a method is called, and because can't pass method as a parameter in Java, so we had to put the callback method in the interface (why not class & # 63; Not an abstract class ? It's the interface ? You can solve this problem by finding out the similarities and differences between abstract classes and interfaces. If there is an interface, it is implemented by the class, and then, as long as the object of the interface is assigned to the object of the implementation class, the object of the interface can call that method. One of the important things to understand here is polymorphic, and the polymorphic knowledge used here is that the object of the interface can be assigned to the subclass without any problems and call the subclass's override method (a class has a similar concept).

Further, any class that implements the CallbackInterface interface can be placed after new like this:


public class CallBackTest {
  interface CallBackInterface {
    void callBackMethod();
  }

  class CallBackClass implements CallBackInterface {

    @Override
    public void callBackMethod() {
      System.out.println("hello");
    }
  }

  class Controller {
    private CallBackInterface cbitf;
    //This Boolean is only used to simulate an event and has no practical value
    public boolean somethingHappend;
    //It is possible to simply take the CallBackClass as an argument and omit the definition of the interface
    //But doing so is like writing the callback function directly into the calling function
    //If you don't understand, just understand "convention" and "caller no matter how the callback function is implemented"
    public Controller(CallBackInterface itfs) {
      somethingHappend = true;
      this.cbitf = itfs;
    }

    public void doSomething() {
      if(somethingHappend) {
        cbitf.callBackMethod();
      }
    }
  }

  public void showCallBack() {
    CallBackClass cbc = new CallBackClass();
    Controller ctrlr = new Controller(cbc);
    ctrlr.doSomething();
    //I could have written it on a line like that
    // new Controller(new CallBackClass()).doSomething();
  }
}

Last but not least, this kind of application is often encountered in android, and I came across it when I was learning android.

Above is the personal callback function understanding and use method, hope you can like.


Related articles: