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

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.  * 定义一套标准方法接口 
  5.  * @author Administrator 
  6.  * 
  7.  */  
  8. interface WindowListener{  
  9.     public void open();  
  10.       
  11. }  
  12.   
  13.   
  14. public class Window {  
  15.       
  16.     private WindowListener windowListener;  
  17.       
  18.     public void registerWindowListener(WindowListener windowListener){  
  19.         this.windowListener = windowListener;  
  20.     }  
  21.       
  22.     public void show(){  
  23.         if(windowListener!=null){  
  24.             windowListener.open();  
  25.         }  
  26.     }  
  27. }  
  28.   
  29. /*      //使用方式如下: 
  30.  *      Window window = new Window(); 
  31.          
  32.         //多线程的Runnable就属于一个回调接口,也就是别的类定义规则,你实现传入,由别人调用你的方法罢了  
  33.           
  34.         window.registerWindowListener(new WindowListener() { 
  35.             @Override 
  36.             public void open() { 
  37.                 System.out.println("打开"); 
  38.             } 
  39.         }); 
  40.          
  41.         window.show(); 
  42. */  


(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.   
  5.     public EventNotifier(InterestingEvent event) {  
  6.         ie = event;  
  7.         somethingHappened = false;  
  8.     }  
  9.   
  10.     public void doWork() {  
  11.         if (somethingHappened) {  
  12.             // 事件发生时,通过调用接口的这个方法来通知  
  13.             ie.interestingEvent();  
  14.         }          
  15.     }  
  16. }  

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

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

以上是通过一个非常简单的例子来说明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.       
  4.     public void regist(CallMe callMe){  
  5.         callMes.add(callMe);  
  6.     }  
  7.       
  8.     public void doWork(){  
  9.         for(CallMe callMe: callMes) {  
  10.             callMe.interestingEvent("sample event");  
  11.         }  
  12.     }      
  13. }  
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.   
  7.         // regiest  
  8.         ren.regist(a);  
  9.         ren.regist(b);  
  10.           
  11.         // test  
  12.         ren.doWork();          
  13.     }  
  14. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值