The callback function, first seen in the Java programming idea, and later the Observer pattern also applied the idea of the callback function. But has been no attention, and finally in an interview at the time of the loss, more and more understand why many people say Java programming ideas this book to look over and over, thoroughly understand!
First search on the network a lot of articles about what is a callback function, see Foggy. Later, the step by step decomposition finally understood, recorded as follows.
Extract the definition from Wikipedia: (read this article to understand the definition)
in the Computer Programming The callback function , or callback, refers to the function Parameters a piece of code passed to another Executable Code of the References . This design allows the underlying code to invoke subroutines defined at the top level .
In Java, we use interfaces to implement callbacks.
The explanations in this article are very well defined: http://blog.csdn.net/sweetwxh/article/details/2067139
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.
[Java]View Plaincopy
- 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.
[Java]View Plaincopy
- Public interface Mycallinterface {
- public void Printname ();
- }
3. The third is the definition of programmer b written by program B
[Java]View Plaincopy
- Public class Client implements Mycallinterface {
- @Override
- public void Printname () {
- System.out.println ("This is theclient Printname method");
- }
- }
4. Test the following
[Java]View Plaincopy
- 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.
[Java]View Plaincopy
- 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 theclient Printname method");
- }
- });
- Caller.call ();
- }
- }
Read the above article, directly read the following article: http://kidult.iteye.com/blog/148982
The above is the full understanding of the callback function.
callback function for a thorough understanding of Java