java 委托 事务,Java中的委托与回调

本文探讨了Java中回调和委托的概念。回调是一种将可执行代码作为参数传递的技术,而委托则是对象内部的策略,独立于调用方式。文章通过示例解释了两者的差异,并指出在某些情况下,回调和委托可能同时使用,因为它们各自服务于不同的目的。在回调中,代码用于通知调用者,而在委托中,方法的行为依赖于委托的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have some misunderstanding about terms of delegates and callbacks in Java.

class MyDriver {

public static void main(String[] argv){

MyObject myObj = new MyObject();

// definition of HelpCallback omitted for brevity

myObj.getHelp(new HelpCallback () {

@Override

public void call(int result) {

System.out.println("Help Callback: "+result);

}

});

}

}

class MyObject {

public void getHelp(HelpCallback callback){

//do something

callback.call(OK);

}

}

How to implement then another one?

解决方案

In computer programming, a callback is a reference to a piece of executable code that is passed as an argument to other code.

So let's look at the executable code:

public void getHelp(HelpCallback callback){

//do something

callback.call(OK);

}

Here, the callback argument is a reference to an object of type HelpCallback. Since that reference is passed in as an argument, it is a callback.

An example of delegation

Delegation is done internally by the object - independent of how the method is invoked. If, for example, the callback variable wasn't an argument, but rather an instance variable:

class MyDriver {

public static void main(String[] argv){

// definition of HelpStrategy omitted for brevity

MyObject myObj = new MyObject(new HelpStrategy() {

@Override

public void getHelp() {

System.out.println("Getting help!");

}

});

myObj.getHelp();

}

}

class MyObject {

private final HelpStrategy helpStrategy;

public MyObject(HelpStrategy helpStrategy) {

this.helpStrategy = helpStrategy;

}

public void getHelp(){

helpStrategy.getHelp();

}

}

... then it would be delegation.

Here, MyObject uses the strategy pattern. There are two things to note:

The invocation of getHelp() doesn't involve passing a reference to executable code. i.e. this is not a callback.

The fact that MyObject.getHelp() invokes helpStrategy.getHelp() is not evident from the public interface of the MyObject object or from the getHelp() invocation. This kind of information hiding is somewhat characteristic of delegation.

Also of note is the lack of a // do something section in the getHelp() method. When using a callback, the callback does not do anything relevant to the object's behavior: it just notifies the caller in some way, which is why a // do something section was necessary. However, when using delegation the actual behavior of the method depends on the delegate - so really we could end up needing both since they serve distinct purposes:

public void getHelp(HelpCallback callback){

helpStrategy.getHelp(); // perform logic / behavior; "do something" as some might say

if(callback != null) {

callback.call(); // invoke the callback, to notify the caller of something

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值