The basic mechanisms are similar, but the semantics are different. Both callbacks and event handlers are called asynchronously .
The callback function is usually passed explicitly from the call procedure to request some information. The information returns after some time, passed as arguments back to the callback of the called party. At this time, the call procedure completes your business. Often a callback is a closure - syntactically inside the calling routine and often unnamed (anonymous). This may look a bit like in javascript:
function caller() { someLibrary.getMeSomething(arg1, arg2, function(returnedData) {
Thus, the called party (someLibrary.getMeSomething) is provided with an anonymous callback function, and after a while this function is called using the returned data. The callback is like a one-time event to a single receiver.
Event handlers are also "returned", but they are usually used for a long period for several events, such as mouse clicks, network events, etc. In addition, several objects may be interested in the same event. For these reasons, you usually “subscribe” or “register” for events in the installation code (for example, initializing objects), and the event handler is usually a named method. Typically, each type of event is also identified as a constant or string.
So, in Python, it might look like this:
class MyUIClass: def __init__(self): someUILib.register(someUILib.events.MOUSE_CLICK, self.my_mouse_click_handler); def my_mouse_click_handler(self, eventInfo):
pwray Sep 08 '14 at 3:31 a.m. 2014-09-08 03:31
source share