Use Java to implement callback routines

Source: Internet
Author: User

Developers familiar with the MS-Windows and XWindowSystem 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:

PublicinterfaceInterestingEvent

{

// This is only a common method. Therefore, if necessary,

// It can return values or receive parameters.

PublicvoidinterestingEvent ();

}

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. This method is much better than using the data fields of the widget to accommodate uncontrollable C Functions of object pointers when using C ++ code for Motif.

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

PublicclassEventNotifier

{

PrivateInterestingEventie;

PrivatebooleansomethingHappened;

PublicEventNotifier (InterestingEventevent)

{

// Save the event object for future use.

Ie = event;

// No event to be reported.

SomethingHappened = false;

}

//...

PublicvoiddoWork ()

{

// 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.

PublicclassCallMeimplementsInterestingEvent

{

PrivateEventNotifieren;

PublicCallMe ()

{

// Create an event notification program and pass its reference to it.

En = newEventNotifier (this );

}

// Define the actual processing program for the event.

PublicvoidinterestingEvent ()

{

// Oh! Events of interest must have occurred!

// Execute some operations...

}

//...

}

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.