你好,今天我要和大家分享一些東西,舉例來說這個在JavaScript中用的很多。我要講講回調(callbacks)。你知道什么時候用,怎么用這個嗎?你真的理解了它在java環境中的用法了嗎?當我也問我自己這些問題,這也是我開始研究這些的原因。這個背后的思想是控制反轉( PS:維基百科的解釋是控制反轉(Inversion of Control,縮寫為IoC),是面向對象編程中的一種設計原則,可以用來減低計算機代碼之間的耦合度。)這個范例描述了框架(framework)的工作方式,也以“好萊塢原則:不要打電話給我們,我們會打給你("Hollywood principle - Don't call me, we will call you)”為人們所熟知。
簡單的Java里的回調模式來理解它,具體的例子在下面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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模型:
回調接口:
讓我們首先定義回調接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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); } } |
在我們的例子中系統定時器支持回調方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
// 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()); } } } |
最后是我們虛擬簡單的例子中的網站時間更新器:
1
2
3
4
5
6
7
8
9
10
|
// 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