Understanding of Java Callback functions

Source: Internet
Author: User
Tags function prototype

The so-called callback is that the client program C calls a function A in the service program s, and then s at some point in turn calls a function B in C, for C, the B is called a callback function. For example, a window procedure function under Win32 is a typical callback function. Generally speaking, C does not call B,c itself to provide B's purpose is to let s to invoke it, and C has to provide. Since s does not know B name provided by C, S will contract B's interface specification (function prototype), and then by C advance through S's a function R tells s itself to use the B function, this process is called the callback function registration, R is called the registration function. Both the Web service and the Java RMI use a callback mechanism to access the remote server program.


Here's a popular example:
One day, I call to ask you questions, of course, is a problem, ^_^, you can not think of a solution, I can't hold the phone in there silly, so we agreed: Wait until you come up with a way to call me, so I hung off the phone to do other things. After xx minutes, my cell phone rang, you cheerfully said that the problem has been taken care of, should be treated as such. The story ends here. This example illustrates the programming pattern of asynchronous + callback. Which, you later on the phone to tell me the result is a "callback" process; my mobile phone number must be told you before, this is the registration callback function, my mobile phone number should be valid and the phone can receive your call, this is the callback function must conform to the interface specification.


Through the above personal feel to the callback more application is to combine asynchronous. For example: In Ajax, JS communicates asynchronously through components and servers.

Cases:

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. Objective to achieve. In C + +, to use a callback function, the dropped function needs to tell the caller their own pointer address, but there is no pointer in Java, what to do? We can implement the definition callback function through the interface (interface).
Let's say I'm programmer a, and here's my program A:

     Public class Caller      {          public  mycallinterface mc;                  Public void Setcallfuc (Mycallinterface MC)          {             this. mc= MC;          }                   Public void Call () {             this. Mc.method ();          }      }      

I also need to define an interface so that programmer B implements the interface based on my definition of programming.

     Public Interface Mycallinterface      {          publicvoid  method ();            }  


So, programmer B only needs to implement this interface to achieve the purpose of the callback:

     Public class Implements Mycallinterface      {          publicvoid  method ()          {             System.out.println ("callback");          }                  Public Static void Main (String args[])          {             new  Caller ();             CALL.SETCALLFUC (new  B ());             Call.call ();          }      }  

Understanding of Java callback functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.