如何实现涉及异步调用的代理模式?
如何实现涉及异步调用的代理模式?
举个例子,如果我想获取一些东西,我会首先检查该对象是否驻留在内存中,如果不是,那么我将发出http请求来检索它(这是异步调用)。
Customer customer;
customer = CustomerDAO.getCustomerByName("John");
并在 CustomerDAO.getCustomerByName("John"); 内
Customer getCustomerByName(String name)
{
int age = 40;
if (map.contains(name))
{
Customer customer = map.get(name);
customer.age = age;
return customer;
}
else
{
makeRequestAsnyc(name, callback);
}
}
然而,由于它是异步的,它破坏了程序的流程。此外, getCustomers 中的任何局部变量也必须传递给回调函数。关于使用异步实现代理模式有什么建议吗?谢谢。
How to implement proxy pattern involving async call?
Just for example, if I want to get something, I will first check if the object resides in memory, if it's not, then I will make http request to retrieve it (which is async call).
Customer customer;
customer = CustomerDAO.getCustomerByName("John");
and inside the CustomerDAO.getCustomerByName("John");
Customer getCustomerByName(String name)
{
int age = 40;
if (map.contains(name))
{
Customer customer = map.get(name);
customer.age = age;
return customer;
}
else
{
makeRequestAsnyc(name, callback);
}
}
However since it's async, it breaks the flow of the program. Also any local variables in getCustomers have to be passed to callback function too. Any suggestion of implementing Proxy pattern with Async? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这又如何呢? (抱歉,我猜测是“回调”类)
一般来说,当您已经有了异步 API 时,您不想引入阻塞方法。
What about this? (Sorry, I'm guessing about the 'Callback' class)
In general you don't want to introduce blocking methods when you have an asynch API already.