理解Java当中的回调机制(翻译)
作者:mdxy-dxy 时间:2023-03-15 04:21:00
你好,今天我要和大家分享一些东西,举例来说这个在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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java TreeSet类的简单理解和使用
![](https://img.aspxhome.com/file/2023/2/77952_0s.jpg)
JAVA图片水印开发案例详解
![](https://img.aspxhome.com/file/2023/9/129989_0s.jpg)
Java矩阵连乘问题(动态规划)算法实例分析
![](https://img.aspxhome.com/file/2023/8/65598_0s.jpg)
Android四大组件之broadcast广播使用讲解
![](https://img.aspxhome.com/file/2023/8/86008_0s.png)
C++ boost scoped_ptr智能指针详解
![](https://img.aspxhome.com/file/2023/7/117767_0s.png)
Android中Json数据读取与创建的方法
![](https://img.aspxhome.com/file/2023/3/138963_0s.png)
java Hibernate多对多映射详解及实例代码
![](https://img.aspxhome.com/file/2023/2/61052_0s.png)
Android高级动画篇之SVG矢量动画范例
![](https://img.aspxhome.com/file/2023/0/139010_0s.gif)
Android实现读取NFC卡的编号
C#中SQL Command的基本用法
Maven build 命令介绍的使用详解
![](https://img.aspxhome.com/file/2023/7/125517_0s.png)
如何实现Java中一个简单的LinkedList
Android仿微信键盘切换效果
![](https://img.aspxhome.com/file/2023/9/138919_0s.jpg)
C#中if语句使用概述
Spring Boot如何使用Spring Security进行安全控制
使用maven profile指定配置文件打包适用多环境的方法
![](https://img.aspxhome.com/file/2023/7/64507_0s.png)
JAVA中使用FTPClient实现文件上传下载实例代码
C#中实现Json序列化与反序列化的几种方式
![](https://img.aspxhome.com/file/2023/5/71065_0s.png)
Android开发应用第一步 安装及配置模拟器Genymotion
![](https://img.aspxhome.com/file/2023/4/91004_0s.png)
Android实现复制Assets文件到SD卡
![](https://img.aspxhome.com/file/2023/6/139186_0s.jpg)