(翻译)理解Java当中的回调机制
原文地址:http://cleancodedevelopment-qualityseal.blogspot.com/2012/10/understanding-callbacks-with-java.html
你好,今天我要和大家分享一些东西,举例来说这个在JavaScript中用的很多。我要讲讲回调(callbacks)。你知道什么时候用,怎么用这个吗?你真的理解了它在java环境中的用法了吗?当我也问我自己这些问题,这也是我开始研究这些的原因。这个背后的思想是控制反转( PS:维基百科的解释是控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。)这个范例描述了框架(framework)的工作方式,也以“好莱坞原则:不要打电话给我们,我们会打给你("Hollywood principle - Don't call me, we will call you)”为人们所熟知。
简单的Java里的回调模式来理解它,具体的例子在下面:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 interface CallBack { 2 void methodToCallBack(); 3 } 4 5 class CallBackImpl implements CallBack { 6 public void methodToCallBack() { 7 System.out.println("I've been called back"); 8 } 9 } 10 11 class Caller { 12 13 public void register(CallBack callback) { 14 callback.methodToCallBack(); 15 } 16 17 public static void main(String[] args) { 18 Caller caller = new Caller(); 19 CallBack callBack = new CallBackImpl(); 20 caller.register(callBack); 21 } 22 }
你可能要问我,什么时候用这个或者会问直接调用和回调机制有什么不同呢?
答案是:好吧,这个例子仅仅向你展示了怎样在java环境中构造这样的回调函数。当然用那种方式使用它毫无意义。让我们现在更加深入具体地研究它。
在它之中的思想是控制反转。让我们用定时器作为现实中的例子。假设你知道,有一个特别的定时器支持每小时回调的功能。准确地说意思是,每小时,定时器会调用你注册的调用方法。
具体的例子:
我们想要每小时更新一次网站的时间,下面是例子的UML模型:
回调接口:
让我们首先定义回调接口:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.util.ArrayList; 2 import java.util.List; 3 4 // For example: Let's assume that this interface is offered from your OS to be implemented 5 interface TimeUpdaterCallBack { 6 void updateTime(long time); 7 } 8 9 // this is your implementation. 10 // for example: You want to update your website time every hour 11 class WebSiteTimeUpdaterCallBack implements TimeUpdaterCallBack { 12 13 @Override 14 public void updateTime(long time) { 15 // print the updated time anywhere in your website's example 16 System.out.println(time); 17 } 18 }
在我们的例子中系统定时器支持回调方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // This is the SystemTimer implemented by your Operating System (OS) 2 // You don't know how this timer was implemented. This example just 3 // show to you how it could looks like. How you could implement a 4 // callback by yourself if you want to. 5 class SystemTimer { 6 7 List<TimeUpdaterCallBack> callbacks = new ArrayList<TimeUpdaterCallBack>(); 8 9 public void registerCallBackForUpdatesEveryHour(TimeUpdaterCallBack timerCallBack) { 10 callbacks.add(timerCallBack); 11 } 12 13 // ... This SystemTimer may have more logic here we don't know ... 14 15 // At some point of the implementaion of this SystemTimer (you don't know) 16 // this method will be called and every registered timerCallBack 17 // will be called. Every registered timerCallBack may have a totally 18 // different implementation of the method updateTime() and my be 19 // used in different ways by different clients. 20 public void oneHourHasBeenExprired() { 21 22 for (TimeUpdaterCallBack timerCallBack : callbacks) { 23 timerCallBack.updateTime(System.currentTimeMillis()); 24 } 25 } 26 }
最后是我们虚拟简单的例子中的网站时间更新器:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // This is our client. It will be used in our WebSite example. It shall update 2 // the website's time every hour. 3 class WebSiteTimeUpdater { 4 5 public static void main(String[] args) { 6 SystemTimer SystemTimer = new SystemTimer(); 7 TimeUpdaterCallBack webSiteCallBackUpdater = new WebSiteTimeUpdaterCallBack(); 8 SystemTimer.registerCallBackForUpdatesEveryHour(webSiteCallBackUpdater); 9 } 10 }