是否可以在 GWT 中重载异步回调?
我在服务器端有一个重载的查询方法。我想知道是否可以根据方法签名重载异步回调?或者是否建议定义两个不同的异步回调?这是我在服务器上的两种方法。
public String fetchInADay(String startTime, String endTime) {}
public String fetchInADay(String startTime, String endTime, String type) {}
顺便说一句,请评论:
如果我我需要进行两个不同的回调,这不是违反OO原则吗?
I have an overloaded query method on the server side. I wanted to know if I can overload the async callback, depending upon the method signature? Or is it advised to define two different asyncallbacks? These are my two methods o the server.
public String fetchInADay(String startTime, String endTime) {}
public String fetchInADay(String startTime, String endTime, String type) {}
As an aside, Please comment:
If I am required to make two different callbacks, isnt this against the principles of OO?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,无法重载异步回调,因为 onSuccess 方法将具有相同的签名。
您可以将同一个 AsyncCallback 对象传递给多个服务,但它无法判断哪个服务或函数调用了它。如果您希望不同的服务调用有不同的行为,则需要两个不同的回调。
There's no way to overload the async callback in this situation because the onSuccess methods will have the same signature.
You can pass the same AsyncCallback object to multiple services, but it won't be able to tell which service or function called it. If you want different behavior for your different service calls, you need two different callbacks.
我假设您计划重用
onSuccess(String result)
中实现的逻辑。无论您调用什么服务方法,这都可以正常工作。您甚至可以在多个调用之间共享同一个实例。由于 javascript 是单线程的,因此多个异步调用的响应(
onSuccess()
调用)不会相互干扰,因此您可以放心。但由于这些调用的异步性质,无法保证其回调的顺序。I assume you plan to reuse the logic implemented in
onSuccess(String result)
. This works fine independent of what service method you call. You could even share the same instance across multiple calls.Since javascript is single threaded you're on the safe side that the responses (
onSuccess()
call) of multiple async calls won't interfere with each other. But because of the asynchronous nature of these calls the order of their callbacks won't be guaranteed.