Equivalent functions of callback functions using Java Interfaces

Source: Internet
Author: User

Summary
Before JAVA supports method pointers, the Java interface cannot provide a good method for implementing callback. If you are used to passing function pointers called in the event-driven programming model, you will like this technique.
Developers familiar with the MS-windows and X Window System event-driven programming models are used to passing function pointers that are called ("Callback") when an event occurs. Java's object-oriented model currently does not support method pointers, so it seems impossible to use such a good mechanism. But we don't have a solution!
Java interfaces support a mechanism to obtain the equivalent functions of callback. The trick is to define a simple interface and declare the method we want to call in this interface.

For example, suppose we want to be notified when an event occurs. We can define an interface:

Public interface interestingevent {
// This is only a common method. Therefore, if necessary,
// It can return values or receive parameters.
Public void interestingevent ();
}

This allows us to control any object of the class that implements this interface. Therefore, we do not need to care about any external type information.CodeIn motif, this method is much better than using the data field of the widget to accommodate the uncontrollable c Functions of object pointers.

The class that sends the event signal must wait for the object that implements the interestingevent interface and call the interestingevent () method as appropriate.

Public class eventnotifier {
Private interestingevent ie;
Private Boolean somethinghappened;

Public eventnotifier (interestingevent event ){
// Save the event object for future use.
Ie = event;
// No event to be reported.
Somethinghappened = false;
Testrecall ();
}

/**
* Callback function
*
* @ Author ken_xu
*/
Public void testrecall (){
Somethinghappened = true;
Dowork ();
}

//...
Public void dowork (){
// Check the predicates set elsewhere.
If (somethinghappened ){
// Send an event signal by calling this method of the interface.
Ie. interestingevent ();
}
//...
}

//...
}

 

In the above example, I used the somethinghappened predicate to track whether an event should be triggered. In many cases, calling this method is sufficient to ensure that a signal is sent to interestingevent.

The code for Receiving Event Notifications must implement the interestingevent interface and pass its reference to the Event Notification.Program.

Public class callme implements interestingevent {
Private eventnotifier en;

Public callme (){
// Create an event notification program and pass its reference to it.
En = new eventnotifier (this );
}

Int donum = 0;

// Define the actual processing program for the event.
Public void interestingevent (){
// Oh! There must be a good event!
// Execute some operations...
System. Out. println ("donum =" + donum ++ );
}
//...
}

This is all the work to be done. I hope this simple Java usage will make you more confident to switch to Java.
Here I add a test function.
Public class testcallme {

/**
* @ Param ARGs
* @ Author ken_xu
*/
Public static void main (string [] ARGs ){
Callme CM = new callme ();
System. Out. println ("==== ");
}

}

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.