1-http://en.wikipedia.org/wiki/Callback_(computer_programming)
In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.
http://www.xyzws.com/Javafaq/how-to-implement-callback-functions-in-java/157
How to implement callback functions in Java?
Writing callback functions
Some Win32 API functions require a function pointer as one of the parameters. The Windows API function may then call the argument function, possibly at a later time when some event occurs. This technique is called acallback function . Examples include window procedures and the callbacks you set up during a print operation (you give the print spooler the address of your callback function so it can update the status and possibly interrupt printing).
Another example is the EnumWindows( ) API function that enumerates all top-level windows currently present in the system. EnumWindows( ) takes a function pointer, then traverses a list maintained internally by Windows. For every window in the list, it calls the callback function, passing the window handle as an argument to the callback.
4-http://docs.jquery.com/How_jQuery_Works#Callback_and_Functions
5-Query 1.3 with PHP
However, in JavaScript, you can call some functions and have them return their results to a "callback" function a few seconds later.
6-Including Ajax Functionality in a Custom JavaServer Faces Component
Callback function is a function called by an XMLHttpRequest object, the same as it is called from server (normally, client calls functions from server). But XMLHttpRequest object is belong to browser so that callback function is a function called by browser after the main function completed to do some extra tasks.
7-Google Web Toolkit
In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.
http://www.xyzws.com/Javafaq/how-to-implement-callback-functions-in-java/157
How to implement callback functions in Java?
The
callback function
is excutable code that is called through a function pointer. You pass a callback function pointer as an argument to other code, or register callback function pointer in somewhere. When something happens, the callback function is invoked.
C/C++ allow function pointers as arguments to other functions but there are no pointers in Java. In Java, only objects and primitive data types can be passed to methods of a class. Java's support of interfaces provides a mechanism by which we can get the equivalent of callbacks. You should declare an interface which declares the function you want to pass.
The
collections.sort(List list, Comparator c)
is an example of implementing callback function in Java. The c
is an instance of a class which implements compare(e1, e2)
method in the Comparator
interface. It sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator.
Use
inner classes
to define an anonymous callback class
, instantiate an anonymous callback delegate object, and pass it as a parameter all in one line. One of the common usage for inner classes is to implement interfaces in event handling.class SomePanel extends JPanel {
private JButton myGreetingButton = new JButton("Hello");
private JTextField myGreetingField = new JTextField(20);
private ActionListener doGreeting = new ActionListener {
public void actionPerformed(ActionEvent e) {
myGreetingField.setText("Hello");
}
};
public SomePanel() {
myGreetingButton.addActionListener(doGreeting);
// . . . Layout the panel.
}
}
2-http://www.javaworld.com/javatips/jw-javatip10.htmlUsing interfaces to implement the equivalent of callback functions in Java
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 ();
}
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 ();
}
}
}
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...
} //...
}
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.
About the author
Subsisting on caffeine, sugar, and too little sleep, John D. Mitchell has been consulting for most of the last nine years, and has developed PDA software in OO assembly language at Geoworks. He funds his Java addiction by writing compilers, Tcl/Tk, C++, and Java systems. He coauthored the hot new Java book Making Sense of Java and is currently developing a Java compiler.
3-http://www.codeguru.com/java/tij/tij0193.shtmlWriting callback functions
Some Win32 API functions require a function pointer as one of the parameters. The Windows API function may then call the argument function, possibly at a later time when some event occurs. This technique is called acallback function . Examples include window procedures and the callbacks you set up during a print operation (you give the print spooler the address of your callback function so it can update the status and possibly interrupt printing).
Another example is the EnumWindows( ) API function that enumerates all top-level windows currently present in the system. EnumWindows( ) takes a function pointer, then traverses a list maintained internally by Windows. For every window in the list, it calls the callback function, passing the window handle as an argument to the callback.
4-http://docs.jquery.com/How_jQuery_Works#Callback_and_Functions
A callback is a function that is passed as an argument to another function and is executed after its parent function has completed. The special thing about a callback is that functions that appear after the "parent" can execute before the callback executes. Another important thing to know is how to properly pass the callback. This is where I have often forgotten the proper syntax.
Callback without arguments
For a callback with no arguments you pass it like this:
$.get('myhtmlpage.html', myCallBack);
After GET response completion then myCallBack will be executed.5-Query 1.3 with PHP
However, in JavaScript, you can call some functions and have them return their results to a "callback" function a few seconds later.
6-Including Ajax Functionality in a Custom JavaServer Faces Component
Callback function is a function called by an XMLHttpRequest object, the same as it is called from server (normally, client calls functions from server). But XMLHttpRequest object is belong to browser so that callback function is a function called by browser after the main function completed to do some extra tasks.
7-Google Web Toolkit
0 comments:
Post a Comment