Callback 在Java和C语言中的实现
来源:互联网 发布:货币战争知乎 编辑:程序博客网 时间:2024/05/02 04:30
1. Java
Using interfaces to implement the equivalent of callback functions in Java
www.javaworld.com/javaworld/javatips/jw-javatip10.html
Developers conversant in the event-driven programming model of MS-Windows and the X Window System are accustomed to passing function pointers that are invoked (that is, "called back") when something happens. Java's object-oriented model does not currently support method pointers, and thus seems to preclude using this comfortable mechanism. But all is not lost!
Java's support of interfaces provides a mechanism by which we can get the equivalent of callbacks. The trick is to define a simple interface that declares the method we wish to be invoked.
For example, suppose we want to be notified when an event happens. We can define an interface:
public interface InterestingEvent
{
// This is just a regular method so it can return something or
// take arguments if you like.
public void interestingEvent ();
}
This gives us a grip on any objects of classes that implement the interface. So, we need not concern ourselves with any other extraneous type information. This is much nicer than hacking trampoline C functions that use the data field of widgets to hold an object pointer when using C++ code with Motif.
The class that will signal the event needs to expect objects that implement the InterestingEvent interface and then invoke the interestingEvent() method as appropriate.
public class EventNotifier
{
private InterestingEvent ie;
private boolean somethingHappened;
public EventNotifier (InterestingEvent event)
{
// Save the event object for later use.
ie = event;
// Nothing to report yet.
somethingHappened = false;
}
//...
public void doWork ()
{
// Check the predicate, which is set elsewhere.
if (somethingHappened)
{
// Signal the even by invoking the interface's method.
ie.interestingEvent ();
}
//...
}
// ...
}
In that example, I used the somethingHappened predicate to track whether or not the event should be triggered. In many instances, the very fact that the method was called is enough to warrant signaling the interestingEvent().
The code that wishes to receive the event notification must implement the InterestingEvent interface and just pass a reference to itself to the event notifier.
public class CallMe implements InterestingEvent
{
private EventNotifier en;
public CallMe ()
{
// Create the event notifier and pass ourself to it.
en = new EventNotifier (this);
}
// Define the actual handler for the event.
public void interestingEvent ()
{
// Wow! Something really interesting must have occurred!
// Do something...
}
//...
}
That's all there is to it. I hope use this simple Java idiom will make your transition to Java a bit less jittery.
- Callback 在Java和C语言中的实现
- Callback在C/C++中的实现
- Callback在C\C++中的实现
- 单链表在C和JAVA中的实现
- 面向对象语言中的callback(C++)
- ++i i++ 在C语言和java中的区别
- CallBack 的理解和java实现
- C语言中的回调函数(Callback Function)
- C语言中的回调函数(Callback Function)
- 关于i=i++在c语言和java语言中的差异原因分析
- java中的callback
- Java 中的callback机制
- 前置声明以及在 Callback 实现中的应用
- 在Android(Java)开发中如何实现类似C语言中的中断程序
- const 在C语言和C++语言中的区别
- URLDecode 和 URLEncode 在 C 中的实现
- Java thread中的callback和Future的简单使用
- 在c语言环境下实现 C++中的getline函数 即在c语言中按行读取
- 使用/sys文件系统访问Linux内核
- libnids使用
- javascript面向对象
- 第一次发帖
- 标准C库的学习-P.J.PLAUCER The Standard C Library读书笔记(一)
- Callback 在Java和C语言中的实现
- java网络编程之UDP
- C/Java 多线程
- js -计算器
- IE6 中的最大最小寬度和高度 css 高度 控制(兼容版本)
- 正则表达式基础知识
- mysql导入导出命令
- 正则表达式
- c# 生成word文档例子