回调函数(callback)浅析

来源:互联网 发布:朴尔因子怎么样 知乎 编辑:程序博客网 时间:2024/05/19 09:47

编程一段时间之后,都会或多或少的接触到“回调函数”的概念,我对这个概念的理解也是浅尝辄止,就此分享一些浅见。

"软件模块之间总是存在着一定的接口,从调用方式上,可以把他们分为三类:同步调用、回调和异步调用。同步调用是一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用;回调是一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口;异步调用是一种类似消息或事件的机制,不过它的调用方向刚好相反,接口的服务在收到某种讯息或发生某种事件时,会主动通知客户方(即调用客户方的接口)。回调和异步调用的关系非常紧密,通常我们使用回调来实现异步消息的注册,通过异步调用来实现消息的通知。同步调用是三者当中最简单的,而回调又常常是异步调用的基础,因此,下面我们着重讨论回调机制在不同软件架构中的实现。"[1]

关于回调,我的个人感觉就是B定义了接口或者是模板,A作为调用者自己实现它,但是A将自己的实现嵌入了B的环境中。抽象一点,其实就是A的定义域嵌入到B的执行域。大部分时候,这两个是在一起的。

Java语言实现

[java] view plain copy
  1. //: innerclasses/Callbacks.java  
  2. // Using inner classes for callbacks  
  3. package innerclasses;  
  4. import static net.mindview.util.Print.*;  
  5.   
  6. interface Incrementable {  
  7.   void increment();  
  8. }  
  9.   
  10. // Very simple to just implement the interface:  
  11. class Callee1 implements Incrementable {  
  12.   private int i = 0;  
  13.   public void increment() {  
  14.     i++;  
  15.     print(i);  
  16.   }  
  17. }     
  18.   
  19. class MyIncrement {  
  20.   public void increment() { print("Other operation"); }  
  21.   static void f(MyIncrement mi) { mi.increment(); }  
  22. }     
  23.   
  24. // If your class must implement increment() in  
  25. // some other way, you must use an inner class:  
  26. class Callee2 extends MyIncrement {  
  27.   private int i = 0;  
  28.   public void increment() {  
  29.     super.increment();  
  30.     i++;  
  31.     print(i);  
  32.   }  
  33.   private class Closure implements Incrementable {  
  34.     public void increment() {  
  35.       // Specify outer-class method, otherwise  
  36.       // you'd get an infinite recursion:  
  37.       Callee2.this.increment();  
  38.     }  
  39.   }  
  40.   Incrementable getCallbackReference() {  
  41.     return new Closure();  
  42.   }  
  43. }     
  44.   
  45. class Caller {  
  46.   private Incrementable callbackReference;  
  47.   Caller(Incrementable cbh) { callbackReference = cbh; }  
  48.   void go() { callbackReference.increment(); }  
  49. }  
  50.   
  51. public class Callbacks {  
  52.   public static void main(String[] args) {  
  53.     Callee1 c1 = new Callee1();  
  54.     Callee2 c2 = new Callee2();  
  55.     MyIncrement.f(c2);  
  56.     Caller caller1 = new Caller(c1);  
  57.     Caller caller2 = new Caller(c2.getCallbackReference());  
  58.     caller1.go();  
  59.     caller1.go();  
  60.     caller2.go();  
  61.     caller2.go();  
  62.   }   
  63. /* Output: 
  64. Other operation 
  65. 1 
  66. 1 
  67. 2 
  68. Other operation 
  69. 2 
  70. Other operation 
  71. 3 
  72. *///:~  
上述例子来自“Think in Java”第四版。这里使用了内部类(inner class)和接口(interface)来实现回调。

Perl语言实现

[plain] view plain copy
  1. use File::Find;  
  2.   
  3. sub create_find_callbacks_that_sum_the_size {  
  4.     my $total_size = 0;  
  5.     return ( sub { $total_size += -s if -f }, sub { return $total_size } );  
  6. }  
  7.   
  8. my ( $count_em, $get_results ) = create_find_callbacks_that_sum_the_size();  
  9.   
  10. find( $count_em, "bin" );  
  11.   
  12. my $total_size = &$get_results();  
  13.   
  14. print "total size of bin is $total_size \n";  
上述例子来自“Learning Perl Objects, References & Modules”,Perl里面其实用了闭包(closure)来实现了回调。

参考:

  1. http://hi.baidu.com/zhuyipeng/blog/item/863fefdb7c736c63d1164eec.html
  2. http://blog.csdn.net/lcqtdwj/article/details/6710355
  3. http://blog.csdn.net/mac_philips/article/details/6058946


本文转载自:http://blog.csdn.net/autofei/article/details/7079202

0 0