Java callback function

Source: Internet
Author: User

The callback refers to A function A in service program S called by client program C, and then S calls A function B in C at A certain time. For C, this B is called a callback function. For example, the window procedure function under Win32 is a typical callback function. Generally, C does not call B by itself. C provides B for S to call it, and C has to provide it. Because S does not know who the B surname provided by C is, S will agree with the interface specification (function prototype) of B ), then C tells S to use the B function through a function R of S in advance. This process is called the registration of the callback function, and R is called the registration function. The callback mechanism is used by Web Service and Java RMI to access remote server programs.

The following is a common example:
One day, I called to ask you a question. Of course it was a problem. ^ _ ^. You couldn't figure out a solution at the moment, and I couldn't wait there with a phone. So we agreed: after you come up with a solution, you can call me to notify me. In this case, I will stop calling to do other things. After XX minutes, my cell phone rang, and you said with high enthusiasm that the problem had been fixed, so you should handle it like this. The story ends here. This example illustrates the programming mode of "Asynchronous + callback. When you call a mobile phone to tell me that the result is a "Callback" process. My mobile phone number must be told before. This is the registration callback function; my mobile phone number should be valid and the mobile phone can receive your call. This is the callback function that must comply with the interface specifications.

I personally felt that more callback applications are combined with Asynchronization. For example, js in Ajax communicates with the server asynchronously through components.
 
Example:
Programmer A writes a program (program A), which reserves the callback function interface and encapsulates the program. Programmer B wants a to call a method in program B, So he calls back the method in B through the interface in. . In C/C ++, a callback function is used. If a function is dropped, the caller must be notified of its own pointer address, but there is no pointer in JAVA. What should I do? We can use interfaces to define callback functions.
Suppose I am A programmer, and the following is my program:
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 can write the program implementation interface according to my definition.
Public interface MyCallInterface
{
Public void method ();

}
As a result, programmer B only needs to implement this interface to achieve the purpose of callback:
Public class B implements MyCallInterface
{
Public void method ()
{
System. out. println ("Callback ");
}

Public static void main (String args [])
{
Caller call = new Caller ();
Call. setCallfuc (new B ());
Call. call ();
}
}

 

This article is from 2332615"

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.