Implementing a callback routine in a Java program

Source: Internet
Author: User

Developers familiar with the ms-windows and X Window System event-driven programming models are accustomed to passing function pointers that are invoked (that is, "callback") when an event occurs. The Java object-oriented model does not currently support method pointers, so it seems impossible to use this very good mechanism. But we don't have any idea!

Java interface Support provides a mechanism for obtaining the equivalent functionality of callbacks. The trick is to define a simple interface and declare the method that we want to invoke in that interface.

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

public interface InterestingEvent
{
// 这仅是一个常规方法。因此如果需要,
// 它可有返回值,也可接收参数。
public void interestingEvent ();
}

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

The class that emits the event signal must wait for the object that implements the Interestingevent interface and call the Interestingevent () method at the appropriate time.

public class EventNotifier
{
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier (InterestingEvent event)
{
// 保存事件对象以备后用。
ie = event;
// 还没有要报告的事件。
somethingHappened = false;
}
//...
public void doWork ()
{
// 检查在别处设置的谓词。
if (somethingHappened)
{
// 通过调用接口的这个方法发出事件信号。
ie.interestingEvent ();
}
//...
}
// ...
}

In the example above, I use 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 the Interestingevent ().

The code that you want to receive event notifications must implement the Interestingevent interface and pass its own reference to the event notification program.

public class CallMe implements InterestingEvent
{
private EventNotifier en;
public CallMe ()
{
// 创建事件通知程序,并将自身引用传递给它。
en = new EventNotifier (this);
}
// 为事件定义实际的处理程序。
public void interestingEvent ()
{
// 噢!必定发生了感兴趣的事件!
// 执行某些操作 ...
}
//...
}

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.