inner classes - callback example

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/85857854

With a callback, some other object is given a piece of information that allows it to call back into the originating object at some later point.

Here's an example:

// innerclasses/Callbacks.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Using inner classes for callbacks
// {java innerclasses.Callbacks}
package innerclasses;

interface Incrementable {
  void increment();
}

// Very simple to just implement the interface:
class Callee1 implements Incrementable {
  private int i = 0;

  @Override
  public void increment() {
    i++;
    System.out.println(i);
  }
}

class MyIncrement {
  public void increment() {
    System.out.println("Other operation");
  }

  static void f(MyIncrement mi) {
    System.out.println("MyIncrement f().");
    mi.increment();
  }
}

// If your class must implement increment() in
// some other way, you must use an inner class:
class Callee2 extends MyIncrement {
  private int i = 0;

  @Override
  public void increment() {
    super.increment();
    i++;
    System.out.println("Callee2 increment:" + i);
  }

  private class Closure implements Incrementable {
    @Override
    public void increment() {
      // Specify outer-class method, otherwise
      // you'll get an infinite recursion:
      Callee2.this.increment();
      this.increment(); // Exception in thread "main" java.lang.StackOverflowError
    }
  }

  Incrementable getCallbackReference() {
    return new Closure();
  }
}

class Caller {
  private Incrementable callbackReference;

  Caller(Incrementable cbh) {
    callbackReference = cbh;
  }

  void go() {
    callbackReference.increment();
  }
}

public class Callbacks {
  public static void main(String[] args) {
    Callee1 c1 = new Callee1();
    Callee2 c2 = new Callee2();
    MyIncrement.f(c2);
    Caller caller1 = new Caller(c1);
    Caller caller2 = new Caller(c2.getCallbackReference());
    caller1.go();
    caller1.go();
    caller2.go();
    caller2.go();
  }
}
/* My Output:
MyIncrement f().
Other operation
Callee2 increment:1
1
2
Other operation
Callee2 increment:2
Other operation
Callee2 increment:3
*/

The inner class Closure implements Incrementable to provide a hook back into Callee2—but a safe hook. Whoever gets the Incrementable reference can only call increment() and has no other abilities.

The value of the callback is in its flexibility; you can dynamically decide what methods are called at run time. In user interfaces, for example, callbacks are often used everywhere to implement GUI functionality.

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/innerclasses/Callbacks.java

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/85857854