In Windows, the programmer wants the system DLL to call a method written by itself, and then uses the interface of the callback function (CALLBACK) in the DLL to write the program, which is called the callback.
In Java, we use interfaces to implement callbacks.
the so-called callback is that programmer a wrote a program (program a), which reserved a callback function interface, and encapsulated the program. Programmer B wants A to invoke a method in its own program B, so he callbacks his method in B through the interface in a. here is an example.
1. First define a class caller, according to the above definition is programmer a written by program A, this class to save an interface reference.
public class Caller {private Mycallinterface callinterface; Public Caller () {} public void Setcallfunc (Mycallinterface callinterface) {this.callinterface = Callinterface; public void Call () {callinterface.printname (); } }
2. Of course, the definition of the interface is required, in order to facilitate programmer B to write a program based on my definition interface.
Public interface Mycallinterface {public void printname (); }
3. The third is the definition of programmer b written by program B
public class Client implements Mycallinterface {@Override public void Printname () {System.out.print ln ("This is the client Printname method"); } }
4. Test the following
public class Test {public static void main (string[] args) {Caller Caller = new Caller (); Caller.setcallfunc (New Client ()); Caller.call (); } }
See here should understand what is callback, some articles introduced very good, but at first did not understand, is because the 3rd step of the class omitted, directly written anonymous class.
5. Use anonymous classes directly in the test method, eliminating the 3rd step.
Public class test { public static void main ( String[] args) { Caller caller = new caller (); // caller.setcallfunc (New Client ( )); caller.setcallfunc (New MyCallInterface () { public void printname () { system.out.println ("This is the client printname method"); } }); caller.call (); } }
For example, when we use the framework, the general direct invocation of the framework provided by the API is possible, but the callback is different, when the framework does not meet the requirements, we want the framework to invoke its own class method, how to do it? There's no way to change the frame. Many excellent frameworks are almost all related interfaces, we just need to implement the relevant interface, we can complete the registration, and then let the framework to call our own class at the appropriate time, remember when we use struts, when we write the action, we need to inherit the action class, Then implement the Execute () method, write our own business logic code in the Execute () method, and complete the processing of the user request. This makes it possible to guess that the framework and the container provide a large number of callback interfaces to suit individual customization.
I wonder if the above example is understood? We can now imagine the difference between filter and interceptor, the biggest difference between the two is that filter is based on a callback function that requires container support, no container is unable to callback the Dofilter () method, Interceptor is based on the reflection mechanism of Java, and is not related to containers.
To make our function close to perfection, we outsource some of the functionality to others and personalize it, and as for how others do it, the only thing I have to do is define the relevant interface, which allows the underlying code to invoke the subroutine defined by the upper layer.
This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1657853
Java callback function