接口回调

Java是一门面向对象语言,一切皆对象,因此在Java中不存在回调函数这一说法的。由于Java的一切皆对象性质,从而将回调函数这个特性提升到了接口回调。

接口回调是什么?

接口回调:可以把使用某一接口的类创建的对象的引用赋给该接口声明的接口变量,那么该接口变量就可以调用被类实现的接口的方法。实际上,当接口变量调用被类实现的接口中的方法时,就是通知相应的对象调用接口的方法,这一过程称为对象功能的接口回调。

从概念可以看出,接口回调是指一个使用过程,并强调是关于对象功能的使用过程,既然是功能,功能一般就对应着方法体(函数),因此它同样满足与回调函数相似的模型。

(1)接口的定义(里面有方法)

(2)接口的实现 (类去实现这个接口)

(3)调用接口 

将(2)的引用(地址)传递给(3),然后(3)调用(2)中的方法,这一过程称为对象功能的接口回调。

如何使用接口回调?

接口回调,我将其分为两种方式,一种推模式,一种为拉模式。

推模式

接口回调的推模式,指的是,甲方主动将其地址推送给调用者。比如下例:

接口:

public interface GasListener
{
    public void offerGas(String msg);
}
接口实现的甲方:
public class GasCompany implements GasListener
{   
    public void advertiseTo(IndoorsMan man)
    {
        System.out.println("煤气公司:这是我们的联系方式,欢迎来电!");
        man.setListener(this);
    }
    @Override
    public void offerGas(String msg)
    {
        System.out.println("煤气公司接收的订单:"+msg);
    }
}
调用者: 
public class IndoorsMan 
{
    GasListener gListener;
    public void prepareCook()
    {
        System.out.println("宅男:准备下厨做几个花式大菜!");
        System.out.println("宅男:进厨房,烧菜...");
        System.out.println("宅男:刚开火,就发现煤气不足,没办法,只能打电话叫煤气。");
        gListener.offerGas("宅男:送一瓶煤气过来!");
    }   
    public void setListener(GasListener gListener)
    {
        this.gListener = gListener;
    }
}
测试: 
public class Test
{
    public static void main(String[] args)
    {
        IndoorsMan man = new IndoorsMan();
        GasCompany company = new GasCompany();
       
        company.advertiseTo(man);
        man.prepareCook();
    }           
}
GasCompany公司在打广告时,就主动把自身信息告诉了IndoorsMan,当IndoorsMan发现煤气不足这一事件发生时,IndoorsMan就根据接口的引用调用GasCompany服务。 
拉模式
接口回调的拉模式,指的是,调用者自己主动获取甲方的信息。

调用者

public class IndoorsMan 
{
    GasListener gListener;
    public void prepareCook()
    {
        System.out.println("宅男:准备下厨做几个花式大菜!");
        System.out.println("宅男:进厨房,烧菜...");
        System.out.println("宅男:刚开火,就发现煤气不足,没办法,只能打电话叫煤气。");
        gListener.offerGas("宅男:送一瓶煤气过来!");
    }   
    public void setListener(GasListener gListener)
    {
        this.gListener = gListener;
    }
    public void configureGas()
    {
        // 主动获取甲方信息
        setListener(new GasListener()
        {           
            @Override
            public void offerGas(String msg)
            {
                System.out.println("下单内容:"+msg);
            }
        });
    }   
}
测试
public class Test
{
    public static void main(String[] args)
    {
        IndoorsMan man = new IndoorsMan();
        man.configureGas();
        man.prepareCook();
    }           
}
再比如,使用线程Thread时,常常会在构建线程对象时,主动获取Runnable接口的实现。
public class Test
{
    public static void main(String[] args)
    {
        Thread thread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
           
            }
        });
    }           
}
 
扩展:
接口回调机制满足观察者设计模式,观察者将自身地址注册给被观察者,当某一事件发生时,被观察者根据注册的地址调用相应的观察者。

参考地址:http://blog.csdn.net/xiaanming/article/details/8703708

http://www.cnblogs.com/aoguren/p/5544865.html

http://www.importnew.com/19301.html

http://www.cnblogs.com/wangming007/p/5122701.html

http://blog.csdn.net/michael1112/article/details/53573830?locationNum=3&fps=1

http://blog.csdn.net/lvzishen123/article/details/50281667

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接口回调是一种常见的编程模式,用于实现程序之间的通信和交互。在Python中,可以通过函数或者类方法来实现接口回调。 如果使用函数来实现接口回调,可以将一个函数作为参数传递给另一个函数或者类的方法,然后在适当的时候调用这个函数来进行回调操作。 示例代码如下: ```python def callback_function(arg1, arg2): # 执行回调操作 print("Callback function called with arguments:", arg1, arg2) def perform_operation(callback): # 进行一些操作 result = 42 # 调用回调函数 callback("arg1", "arg2") # 使用回调函数 perform_operation(callback_function) ``` 上述代码中,`perform_operation` 函数接收一个回调函数作为参数,并在适当的时候调用这个回调函数。在这个例子中,我们调用 `perform_operation` 函数时会执行回调函数 `callback_function`,并传递两个参数 `"arg1"` 和 `"arg2"`。 如果使用类方法来实现接口回调,可以定义一个接口类,其中包含一个或多个方法,然后创建一个实现该接口的类,并将其实例传递给另一个类的方法进行回调操作。 示例代码如下: ```python class CallbackInterface: def callback_method(self, arg1, arg2): # 执行回调操作 print("Callback method called with arguments:", arg1, arg2) class PerformOperation: def __init__(self, callback): self.callback = callback def perform_operation(self): # 进行一些操作 result = 42 # 调用回调方法 self.callback.callback_method("arg1", "arg2") # 使用回调方法 callback_instance = CallbackInterface() perform_operation = PerformOperation(callback_instance) perform_operation.perform_operation() ``` 上述代码中,`CallbackInterface` 类定义了一个回调方法 `callback_method`。`PerformOperation` 类接收一个实现了 `CallbackInterface` 接口的对象作为参数,并在适当的时候调用该对象的回调方法。 无论是使用函数还是类方法来实现接口回调,关键在于正确地传递回调函数或者实现了回调接口的对象,并在适当的时候进行调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值