Java Reference
In-Depth Information
Closures and Callbacks
In functional programming, a higher order function is an anonymous function that can be treated as a data object.
That is, it can be stored in a variable and passed around from one context to another. It might be invoked in a context
that did not necessarily define it. Note that a higher order function is an anonymous function, so the invoking context
does not have to know its name. A closure is a higher order function packaged with its defining environment. A closure
carries with it the variables in scope when it was defined, and it can access those variables even when it is invoked in a
context other than the context in which it was defined.
In object-oriented programming, a function is called a method and it is always part of a class. An anonymous class
in Java allows a method to be packaged in an object that can be treated much as a higher order function. The object
can be stored in a variable and passed around from one method to another. The method defined in an anonymous
class can be invoked in a context other than the one in which was defined. However, one important difference between
a higher order function and a method defined in an anonymous class is that a higher order function is anonymous,
whereas a method in an anonymous class is named. The invoker of the anonymous class method must know the
method name. An anonymous class carries with it its environment. An anonymous class can use the local variables
and the parameters of a method inside which it is defined. However, Java places a restriction that local variables and
parameters to the method must be effectively final if they are accessed inside an anonymous class.
The callback mechanism can be implemented using anonymous classes and interfaces. In the simplest form, you
register an object, which implements an interface. A particular method is called (back) on the registered object later.
Let's define an interface named Callable with one method of call() , as shown in Listing 2-23.
Listing 2-23. A Callable Interface to Implement a Callback Mechanism
// Callable.java
package com.jdojo.innerclasses;
public interface Callable {
void call();
}
The CallbackTest class illustrates the implementation details of the callback mechanism. The generateCallable()
method is used to generate the object that implements the Callable interface. You also pass an integer to that method
in order to recognize the object that it creates. The register() method registers a Callable object and it stores
the object's reference in an ArrayList so that these object's call() method can be executed later. The callback()
method calls back all registered objects by invoking their call() methods. See Listing 2-24.
Listing 2-24. Implementing Callback Mechanism
// CallbackTest.java
package com.jdojo.innerclasses;
import java.util.ArrayList;
public class CallbackTest {
// To hold all registered Callable objects
private ArrayList<Callable> callableList = new ArrayList<>();
public static void main(String[] args) {
CallbackTest cbt = new CallbackTest();
 
Search WWH ::




Custom Search