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