当前位置: 移动技术网 > IT编程>开发语言>Java > 理解Java当中的回调机制(翻译)

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

原文:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网