English 中文(简体)
嵌套回拨的 Java 模式吗?
原标题:Java pattern for nested callbacks?

我正在寻找一个 Java 模式, 用来进行嵌套式的无阻截方法呼叫。 在我的例子中, 某些客户代码需要不同步地援引一个服务来进行某种使用案例, 而该使用案例的每一个步骤本身都必须不同步地进行( 原因不属于本问题范围 ) 。 想象一下我现有的接口如下:

public interface Request {} 

public interface Response {} 

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

request Response 界面有各种对称执行,即 requestA + ResponseA (客户端提供), requestB + ResponseB (服务内部使用),等等。

处理流程看起来是这样的:

"https://i.sstatic.net/egQJX.png" alt="序列图,显示嵌套回拨。"/ >

从收到每份答复到发出下一份请求之间,还需要进行一些额外的处理(例如,根据以前任何要求或答复中的数值)。

到目前为止,我尝试了两种方法 来在爪哇的编码:

  • anonymous classes: gets ugly quickly because of the required nesting
  • inner classes: neater than the above, but still hard for another developer to comprehend the flow of execution

是否有某种模式可以让该代码更容易读取? 例如,我能否将服务方法表达为一份由某个负责筑巢的框架类别按顺序执行的自足操作清单?

最佳回答
问题回答

我不确定我是否正确理解您的问题。 如果您想要援引一个服务及其完成结果, 需要将它传递给其他可以继续使用结果处理的对象。 您可以查看使用 Compite and pervision 来完成它 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签