Java 回调函数

 1. 什么是回调函数
回调函数(callback Function),顾名思义,用于回调的函数。 回调函数只是一个功能片断,由用户按照回调函数调用约定来实现的一个函数。回调函数是一个工做流的一部分,由工做流来决定函数的调用(回调)时机。回调函数包含下面几个特性:
一、属于工做流的一个部分;
二、必须按照工做流指定的调用约定来申明(定义);
三、他的调用时机由工做流决定,回调函数的实现者不能直接调用回调函数来实现工做流的功能;

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

=======================================================编程

java回调机制:app

软件模块之间老是存在着必定的接口,从调用方式上,能够把他们分为三类:同步调用、回调和异步调用。异步

 

同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用;函数

回 调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口;工具

异步调用:一种相似消息或事件的机制,不过它的调用方向恰好相反,接口的服务在收到某种讯息或发生某种事件时,会主动通知客户方(即调用客户方的接口)。post

回调和异步调用的关系很是紧密:使用回调来实现异步消息的注册,经过异步调用来实现消息的通知。测试

========================================================this

 

用Java里的例子:spa

 

interface ICallBack {
	//须要回调的方法 
	public void postExec();
}

 

另外的一个类:

class FooBar {
	//组合聚合原则
	private ICallBack callBack;

	public void setCallBack(ICallBack callBack) {
		this.callBack = callBack;
		doSth();
	}

	public void doSth() {
		callBack.postExec();
	}
}
  

第二个类在测试类里面,是一个匿名类:

public class test{
	public static void main(String[] args) {
		FooBar foo = new FooBar();
		foo.setCallBack(new ICallBack() {
			public void postExec() {
				System.out.println("在Test类中实现但不能被Test的对象引用,而由FooBar对象调用");
			}
		});
	}
}

上诉的代码:

1.两个类:匿名类和FooBar

2.匿名类实现接口ICallBack(在test测试的main方法中用匿名类的形式实现)

3.FooBar 拥有一个参数为ICallBack接口类型的函数setCallBack(ICallBack o)

4.匿名类运行时调用FooBar中setCallBack函数,以自身传入参数

5.FooBar已取得匿名类,就能够随时回调匿名类中所实现的ICallBack接口中的方法

==================================

1。首先回调方法的概念与“构造方法”的概念是不同的,它不是指java中某个具备特殊意义或用途的方法。
2。称它为方法的“回调”更恰当一些,它是指方法的一种调用方式。任何一个被“回调”的方法,皆可称之为“回调方法”
3。方法的回调一般发生在“java接口”和“抽象类”的使用过程当中。
假设有接口名为 ICallBack 其中有方法名为postExec()
有类Myclass 实现了该接口,也就是必定实现了postExec()这个方法。如今有另外一个类FooBar它有个方法 setCallBack(ICallBack callBack) ,而且setCallBack方法调用了callBack的postExec()方法。
若是如今,咱们使用一个Myclass 的实例myClass,将它做为参数带入到setCallBack(ICallBack callBack)方法中,咱们就说setCallBack(ICallBack callBack)方法回调了myClass的postExec()方法。

 

 

下面使用java回调函数来实现一个测试函数运行时间的工具类:
若是咱们要测试一个类的方法的执行时间,一般咱们会这样作:
java 代码

public   class TestObject {   
    /**   
     * 一个用来被测试的方法,进行了一个比较耗时的循环   
     */    
    public   static   void testMethod(){   
        for ( int i= 0 ; i< 100000000 ; i++){   
               
        }   
    }   
    /**   
     * 一个简单的测试方法执行时间的方法   
     */    
    public   void testTime(){   
        long begin = System.currentTimeMillis(); //测试起始时间    
        testMethod(); //测试方法    
        long end = System.currentTimeMillis(); //测试结束时间    
        System.out.println("[use time]:" + (end - begin)); //打印使用时间    
    }   
       
    public   static   void main(String[] args) {   
        TestObject test=new TestObject();   
        test.testTime();   
    }   
}

你们看到了testTime()方法,就只有"//测试方法"是须要改变的,下面咱们来作一个函数实现相同功能但更灵活:
首先定一个回调接口:
java 代码
public   interface CallBack {   
    //执行回调操做的方法    
    void execute();   
}

而后再写一个工具类:
java 代码
public   class Tools {   
       
    /**   
     * 测试函数使用时间,经过定义CallBack接口的execute方法   
     * @param callBack   
     */    
    public   void testTime(CallBack callBack) {   
        long begin = System.currentTimeMillis(); //测试起始时间    
        callBack.execute(); ///进行回调操做    
        long end = System.currentTimeMillis(); //测试结束时间    
        System.out.println("[use time]:" + (end - begin)); //打印使用时间    
    }   
       
    public   static   void main(String[] args) {   
        Tools tool = new Tools();   
        tool.testTime(new CallBack(){   
            //定义execute方法    
            public   void execute(){   
                //这里能够加放一个或多个要测试运行时间的方法    
                TestObject.testMethod();   
            }   
        });   
    }   
}

你们看到,testTime()传入定义callback接口的execute()方法就能够实现回调功能

==============================================================

若是说匿名内部类的方式不容易理解,能够看下面的例子

其技巧就是:定义一个简单接口,并在该接口中声明咱们要调用的方法。

下面举一个例子:

假定咱们但愿在某个事件发生时获得通知。咱们能够定义一个接口:

/*
* 在某个事件发生时获得通知.
*/
public interface InterestingEvent {
   public void interestingEvent();
}

此接口中的方法,是个没有返回值的也没有任何参数,若是您愿意也能够有返回值,也能够带参数.这就要看具体需求而定.

这使得咱们能够控制实现该接口的类的任何对象。所以,咱们没必要关心任何外部类型信息。与在将 C++ 代码用于Motif 时使用窗口小部件的数据域来容纳对象指针的难以控制的 C 函数相比,这种方法要好得多。

实现接口的代码以下:

public class CallMe implements InterestingEvent {
        public CallMe() {
   }

   public void interestingEvent() {
         System.out.println("发生了打印事件,哈哈"); 
  }

}

public class CallYou implements InterestingEvent {
       public CallYou() {
     }

public void interestingEvent() {
   
      System.out.println("发生了查询事件,哈哈"); 
}

}

发出事件信号的类必须等待实现了 InterestingEvent 接口的对象,并在适当时候调用 interestingEvent() 方法。

public class EventNotifier {
private InterestingEvent ie;
private boolean somethingHappened ;
public EventNotifier() {
   somethingHappened = true ;
}
public void setInterestingEvent(InterestingEvent ie){
   this.ie = ie ;
}
public void doWork(){
   if(somethingHappened){
    ie.interestingEvent();
   }
}

}

下面作一下测试.

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
   CallMe cm = new CallMe();
   CallYou cy = new CallYou();
   EventNotifier en = new EventNotifier();

   en.setInterestingEvent(cm);
   en.doWork();
   en.setInterestingEvent(cy);
   en.doWork();
}

}

此测试在发生指定的调用CalMe事件时,就扫行CallMe下的命令,如发生CallYou事件时,就调用CallYou下的命令.此种方法能够结合Command模式.实现MS-Windows 和 X Window System 事件驱动编程模型

相关文章
相关标签/搜索