Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize callback routine

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you how to use Java to implement callback routines, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Developers familiar with the MS-windows and X Window System event-driven programming models are used to passing pointers to functions that are invoked (i.e.,"callbacks") when certain events occur. Java's object-oriented model does not currently support method pointers, making it seemingly impossible to use this nice mechanism. But we're not helpless!

Java's interface support provides a mechanism for obtaining the functional equivalence of callbacks. The trick is to define a simple interface and declare the method we want to call in that interface.

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

public interface InterestingEvent

{

//This is just a general approach. So if need be,

//It can have a return value or it can receive parameters.

public void interestingEvent ();

}

This allows us to control any object that implements the class of the interface. Therefore, we do not have to care about any external type information. This approach is much better than using C++ code for Motif, which uses the widget's data field to hold the object pointer's unruly C function.

The class that signals the event must wait for an object that implements the interestingEvent interface and call the interestingEvent() method when appropriate.

public class EventNotifier

{

private InterestingEvent ie;

private boolean somethingHappened;

public EventNotifier (InterestingEvent event)

{

//Save event objects for later use.

ie = event;

//There are no incidents to report yet.

somethingHappened = false;

}

//...

public void doWork ()

{

//Check predicates set elsewhere.

if (somethingHappened)

{

//Signal an event by calling this method of the interface.

ie.interestingEvent ();

}

//...

}

// ...

}

In the example above, I used the somethingHappened predicate to track whether an event should be fired. In many cases, calling this method is sufficient to ensure that interestingEvent() is signaled.

Code that wants to receive event notifications must implement the IntestingEvent interface and pass a reference to itself to the event notifier.

public class CallMe implements InterestingEvent

{

private EventNotifier en;

public CallMe ()

{

//Create an event notifier and pass its own reference to it.

en = new EventNotifier (this);

}

//Define the actual handler for the event.

public void interestingEvent ()

{

//Oh! Something interesting must have happened!

//perform certain actions...

}

//...

}

The above is "how to use Java callback routines" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report