理解Java当中的回调机制(翻译)

所属分类: 软件编程 / java 阅读数: 87
收藏 0 赞 0 分享

你好,今天我要和大家分享一些东西,举例来说这个在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

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多