Java 回调函数作用和使用场景

论坛 期权论坛 脚本     
匿名技术用户   2020-12-22 14:30   11   0
1. 什么是回调函数
回调函数(callback Function),顾名思义,用于回调的函数。 回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数。回调函数是一个工作流的一部分,由工作流来决定函数的调用(回调)时机。回调函数包含下面几个特性:
1、属于工作流的一个部分;
2、必须按照工作流指定的调用约定来申明(定义);
3、他的调用时机由工作流决定,回调函数的实现者不能直接调用回调函数来实现工作流的功能;

2. 回调机制

回调机制是一种常见的设计模型,他把工作流内的某个功能,按照约定的接口暴露给外部使用者,为外部使用者提供数据,或要求外部使用者提供数据。

3、使用场景

(1)在必须给别的函数提供接口的时候</p>
(2)在需要定时操作,或者条件操作的时候

4、回调方法测试用例:

(1)多线程的Runnable就属于一个回调接口,也就是别的类定义规则,你实现传入,由别人调用你的方法罢了 。

[java] view plain copy
  1. package com.gy.appsdk.dao;
  2. /**
  3. * 定义一套标准方法接口
  4. * @author Administrator
  5. *
  6. */
  7. interface WindowListener{
  8. public void open();
  9. }
  10. public class Window {
  11. private WindowListener windowListener;
  12. public void registerWindowListener(WindowListener windowListener){
  13. this.windowListener = windowListener;
  14. }
  15. public void show(){
  16. if(windowListener!=null){
  17. windowListener.open();
  18. }
  19. }
  20. }
  21. /* //使用方式如下:
  22. * Window window = new Window();
  23. //多线程的Runnable就属于一个回调接口,也就是别的类定义规则,你实现传入,由别人调用你的方法罢了
  24. window.registerWindowListener(new WindowListener() {
  25. @Override
  26. public void open() {
  27. System.out.println("打开");
  28. }
  29. });
  30. window.show();
  31. */


(2)熟悉MS-Windows和X Windows事件驱动设计模式的开发人员,通常是把一个方法的指针传递给事件源,当某一事件发生时来调用这个方法(也称为“回调”)。Java的面向对象的模型目前不支持方法指针,似乎不能使用这种方便的机制。

Java支持interface,通过interface可以实现相同的回调。其诀窍就在于定义一个简单的interface,申明一个被希望回调的方法。


例如,假定当某一事件发生时会得到通知,我们可以定义一个interface:

[java] view plain copy
  1. public interface InterestingEvent {
  2. // 这只是一个普通的方法,可以接收参数、也可以返回值
  3. public void interestingEvent();
  4. }

这样我们就有了任何一个实现了这个接口类对象的手柄grip。

当一事件发生时,需要通知实现InterestingEvent 接口的对象,并调用interestingEvent() 方法。
[java] view plain copy
  1. class EventNotifier {
  2. private InterestingEvent ie;
  3. private boolean somethingHappened;
  4. public EventNotifier(InterestingEvent event) {
  5. ie = event;
  6. somethingHappened = false;
  7. }
  8. public void doWork() {
  9. if (somethingHappened) {
  10. // 事件发生时,通过调用接口的这个方法来通知
  11. ie.interestingEvent();
  12. }
  13. }
  14. }

在这个例子中,用somethingHappened 来标志事件是否发生。

希望接收事件通知的类必须要实现InterestingEvent 接口,而且要把自己的引用传递给事件的通知者。
[java] view plain copy
  1. public class CallMe implements InterestingEvent {
  2. private EventNotifier en;
  3. public CallMe() {
  4. // 新建一个事件通知者对象,并把自己传递给它
  5. en = new EventNotifier(this);
  6. }
  7. // 实现事件发生时,实际处理事件的方法
  8. public void interestingEvent() {
  9. // 这个事件发生了,进行处理
  10. }
  11. }

以上是通过一个非常简单的例子来说明Java中的回调的实现。

当然,也可以在事件管理或事件通知者类中,通过注册的方式来注册多个对此事件感兴趣的对象。

1. 定义一个接口InterestingEvent ,回调方法nterestingEvent(String event) 简单接收一个String 参数。

[java] view plain copy
  1. interface InterestingEvent {
  2. public void interestingEvent(String event);
  3. }

2. 实现InterestingEvent接口,事件处理类

[java] view plain copy
  1. class CallMe implements InterestingEvent {
  2. private String name;
  3. public CallMe(String name){
  4. this.name = name;
  5. }
  6. public void interestingEvent(String event) {
  7. System.out.println(name + ":[" +event + "] happened");
  8. }
  9. }

3. 事件管理者,或事件通知者

[java] view plain copy
  1. class EventNotifier {
  2. private List<CallMe> callMes = new ArrayList<CallMe>();
  3. public void regist(CallMe callMe){
  4. callMes.add(callMe);
  5. }
  6. public void doWork(){
  7. for(CallMe callMe: callMes) {
  8. callMe.interestingEvent("sample event");
  9. }
  10. }
  11. }

4. 测试

[java] view plain copy
  1. public class CallMeTest {
  2. public static void main(String[] args) {
  3. EventNotifier ren = new EventNotifier();
  4. CallMe a = new CallMe("CallMe A");
  5. CallMe b = new CallMe("CallMe B");
  6. // regiest
  7. ren.regist(a);
  8. ren.regist(b);
  9. // test
  10. ren.doWork();
  11. }
  12. }
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP