Java callback-Java implements callback

Source: Internet
Author: User
Document directory
  • JAVA supports interfaces, and the same callback can be implemented through interfaces. The trick is to define a simple interface and declare a method to be called back.
  • Of course, you can also register multiple objects of interest to this event in the event management or event notification class.

 

The so-calledCallbackThat is, the customer program c calls a function SA in service program s, and then s calls a function CB in C at a certain time. For C, this CB is called a callback function. For example, the window procedure function under Win32 is a typical callback function. Generally, C does not call CB by itself. C provides CB for S to call it, and C has to provide it. Because s does not know who the CB name provided by C is, s will agree with the interface specification (function prototype) of B ), then C tells s to use the CB function through the R function 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.


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 have come up with a solution, you can call me to notify me. In this way, I will stop calling to handle 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 "Asynchronous + callback" programming mode.


Developers familiar with the MS-windows and X Windows event-driven design modes usually pass a method pointer to the event source, this method (also called "Callback") is called when an event occurs "). Java's object-oriented model currently does not support method pointers, and it seems that this convenient mechanism cannot be used.

JAVA supports interfaces, and the same callback can be implemented through interfaces. The trick is to define a simple interface and declare a method to be called back.

For example, if an event is notified, we can define an interface:
Public interface interestingevent {<br/> // This is only a common method. It can receive parameters or return values. <br/> Public void interestingevent (); <br/>}

In this way, we have any controller grip that implements this interface class object.

Notification implementation is required when an event occurs.InterestingeventInterface object, and callInterestingevent ()Method.
Class eventnotifier {<br/> private interestingevent ie; <br/> private Boolean somethinghappened; <br/> Public eventnotifier (interestingevent event) {<br/> Ie = event; <br/> somethinghappened = false; <br/>}< br/> Public void dowork () {<br/> If (somethinghappened) {<br/> // when an event occurs, you can call this method to notify <br/> IE. interestingevent (); <br/>}< br/>}

In this exampleSomethinghappenedTo indicate whether an event occurs.

Classes that want to receive Event Notifications must be implementedInterestingeventAnd pass your reference to the Event Notification.
Public class callme implements interestingevent {<br/> private eventnotifier en; <br/> Public callme () {<br/> // create an Event Notification object, and pass it to it <br/> en = new eventnotifier (this); <br/>}< br/> // when an event occurs, actual event handling method <br/> Public void interestingevent () {<br/> // This event has occurred and is being processed <br/>}< br/>}

The above is a very simple example to illustrate the implementation of callback in Java.

Of course, you can also register multiple objects of interest to this event in the event management or event notification class. 1. Define an Interface Interestingevent, Callback Method Nterestingevent (string event)Simply receive StringParameters.

Interface interestingevent {<br/> Public void interestingevent (string event); <br/>} 

2. Implement the interestingevent interface and event processing class

Class callme implements interestingevent {<br/> private string name; <br/> Public callme (string name) {<br/> This. name = Name; <br/>}< br/> Public void interestingevent (string event) {<br/> system. out. println (name + ": [" + event + "] happened"); <br/>}< br/>} 

3. event manager or event notification recipient

Class eventnotifier {<br/> private list <callme> callmes = new arraylist <callme> (); </P> <p> Public void regist (callme) {<br/> callmes. add (callme); <br/>}</P> <p> Public void dowork () {<br/> for (callme: callmes) {<br/> callme. interestingevent ("sample event"); <br/>}< br/>} 

4. Test

Public class callmetest {<br/> Public static void main (string [] ARGs) {<br/> eventnotifier Ren = new eventnotifier (); <br/> callme A = new callme ("callme a"); <br/> callme B = new callme ("callme B "); <br/> // regiest <br/> Ren. regist (a); <br/> Ren. regist (B); </P> <p> // test <br/> Ren. dowork (); <br/>}< br/>} 

 

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.