嵌套回调的 Java 模式? [英] Java pattern for nested callbacks?

查看:20
本文介绍了嵌套回调的 Java 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种 Java 模式来进行非阻塞方法调用的嵌套序列.就我而言,某些客户端代码需要异步调用服务来执行某些用例,并且该用例的每个步骤本身都必须异步执行(出于本问题范围之外的原因).假设我有如下现有接口:

I'm looking for a Java pattern for making a nested sequence of non-blocking method calls. In my case, some client code needs to asynchronously invoke a service to perform some use case, and each step of that use case must itself be performed asynchronously (for reasons outside the scope of this question). Imagine I have existing interfaces as follows:

public interface Request {} 

public interface Response {} 

public interface Callback<R extends Response> {
    void onSuccess(R response);
    void onError(Exception e);
}

RequestResponse 接口有多种成对的实现,即 RequestA + ResponseA(由客户端)、RequestB + ResponseB(服务内部使用)等

There are various paired implementations of the Request and Response interfaces, namely RequestA + ResponseA (given by the client), RequestB + ResponseB (used internally by the service), etc.

处理流程如下:

在收到每个响应和发送下一个请求之间,需要进行一些额外的处理(例如,基于任何先前请求或响应中的值).

In between the receipt of each response and the sending of the next request, some additional processing needs to happen (e.g. based on values in any of the previous requests or responses).

到目前为止,我已经尝试了两种用 Java 编写代码的方法:

So far I've tried two approaches to coding this in Java:

  • 匿名类:由于需要嵌套而很快变得丑陋
  • 内部类:比上面的更简洁,但其他开发人员仍然难以理解执行流程

是否有一些模式可以使这段代码更具可读性?例如,我是否可以将服务方法表示为一系列自包含操作,这些操作由某个负责嵌套的框架类按顺序执行?

Is there some pattern to make this code more readable? For example, could I express the service method as a list of self-contained operations that are executed in sequence by some framework class that takes care of the nesting?

推荐答案

既然实现(不仅仅是接口)一定不能阻塞,我喜欢你的列表思路.

Since the implementation (not only the interface) must not block, I like your list idea.

设置操作"列表(可能是Futures?),其设置应该非常清晰易读.然后在收到每个响应后,应调用下一个操作.

Set up a list of "operations" (perhaps Futures?), for which the setup should be pretty clear and readable. Then upon receiving each response, the next operation should be invoked.

稍加想象,这听起来像是责任链.这是我想象的一些伪代码:

With a little imagination, this sounds like the chain of responsibility. Here's some pseudocode for what I'm imagining:

public void setup() {
    this.operations.add(new Operation(new RequestA(), new CallbackA()));
    this.operations.add(new Operation(new RequestB(), new CallbackB()));
    this.operations.add(new Operation(new RequestC(), new CallbackC()));
    this.operations.add(new Operation(new RequestD(), new CallbackD()));
    startNextOperation();
}
private void startNextOperation() {
    if ( this.operations.isEmpty() ) { reportAllOperationsComplete(); }
    Operation op = this.operations.remove(0);
    op.request.go( op.callback );
}
private class CallbackA implements Callback<Boolean> {
    public void onSuccess(Boolean response) {
        // store response? etc?
        startNextOperation();
    }
}
...

这篇关于嵌套回调的 Java 模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆