Interface CallbackFactory

All Known Implementing Classes:

public interface CallbackFactory

Instead of implementing a certain callback-interface it is also possible for a callback-value to provide a callback-interface implementation by implementing this interface and have the method getCallback() return an arbitrary object, which will work as a "backup" callback if it implements the callback-interface in case the original callback-value does not. This code-example shows how it works ...

 interface Foo {
   void printFooOutput();

 interface Bar {
   void printBarOutput();

 public class MyTest {

   public test(Foo f, Bar b) {
     f.printFooOutput();                                                 //(1)
     b.printBarOutput();                                                 //(2)

   enum MyEnum implements Foo, CallbackFactory {
     SUPPLIES_BAR(new Bar() {
       public void printBarOutput() {
         System.out.println("Output from supplied bar");                 //(3)
     SUPPLIES_FOO(new Foo() {
       public void printFooOutput() {
         System.out.println("Output from supplied foo is shielded ..."); //(4)

     Object supplied;

     MyEnum(Object supplied) {
       this.supplied = supplied;

     public void printFooOutput() {
       System.out.println("Output from enum-constant " + this            //(5)
           + " that implements interface Foo");

     public Object getCallback() {
       return this.supplied;
The test-class will produce three test-runs ...
Output from "test[SUPPLIES_NONE]":
 Output from enum-constant SUPPLIES_NONE that implements interface Foo
Output from "test[SUPPLIES_BAR]":
 Output from enum-constant SUPPLIES_BAR that implements interface Foo
 Output from supplied bar
Output from "test[SUPPLIES_FOO]":
 Output from enum-constant SUPPLIES_FOO that implements interface Foo
The code f.printFooOutput() (1) consistently produces output through the statement at (5). On the other hand, b.printBarOutput (2) only produces output when the callback-record contains MyEnum-constant SUPPLIES_BAR, which, unlike the other MyEnum-constants, offers a supplied instance that happens to implement callback-interface Bar.
Please also note that f.printFooOutput() (1) does not produce any output from statement (4) when SUPPLIES_FOO is in the the callback-record. This is because SUPPLIES_FOO itself implements Foo and therefore its supplied callback will never be considered when a Foo-method is invoked.

Henrik Kaipe

Method Summary
 Object getCallback()
          Returns the supplied callback

Method Detail


Object getCallback()
Returns the supplied callback

Copyright © 2010-2013 Henrik Kaipe. All Rights Reserved.