如何实现在Java中回调 [英] How to implement callbacks in Java

查看:139
本文介绍了如何实现在Java中回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为CommunicationManager类,这是负责与服务器之间的通信。

I have a class called CommunicationManager which is responsible for communication with server.

它包括的方法登录() onLoginResponse()。在用户的情况下登录方法登录()已被调用,当服务器响应的方法 onLoginResponse()被执行。

It includes methods login() and onLoginResponse(). In case of user login the method login() has to be called and when the server responds the method onLoginResponse() is executed.

我想要做的是绑定用户界面操作。在GUI类我创建了一个名为 mCommunicationManager CommunicationManager的一个实例。从GUI类的登录()方法是简单地由所谓的行

What I want to do is to bind actions with user interface. In the GUI class I created an instance of CommunicationManager called mCommunicationManager. From GUI class the login() method is simply called by the line

mCommunicationManager.login();

我不知道该怎么办,从GUI类中的方法绑定到 onLoginResponse()。例如,如果GUI类包括方法为notifyUser()这显示从theserver收到的邮件。

What I don't know how to do is binding the method from GUI class to onLoginResponse(). For example if the GUI class includes the method notifyUser() which displays the message received from theserver.

我真的AP preciate如果任何人都可以展示如何绑定,以便执行从GUI类中的方法的方法(例如, GUI.notifyUser())上课的时候​​ mCommunicationManager 的实例从服务器接收的消息和方法 CommunicationManager.onLoginResponse()被执行。

I would really appreciate if anyone could show how to bind methods in order to execute the method from GUI class (ex. GUI.notifyUser()) when the instance of the class mCommunicationManager receives the message from the server and the method CommunicationManager.onLoginResponse() is executed.

谢谢!

推荐答案

这里有两种模式,我可以用你看到。一个是发布/订阅或观察者模式皮特提到。我想这可能是你想要什么,但看到这个问题提到绑定以便以后执行的方法,我想我应该提到 Command模式

There's two patterns here I can see you using. One is the publish/subscribe or observer pattern mentioned by Pete. I think this is probably what you want, but seeing as the question mentions binding a method for later execution, I thought I should mention the Command pattern.

命令模式基本上是作为第一类对象,一个变通的事实,java不能治疗方法(函数),它是因此不可能通过他们周围。相反,你创建一个可以被传递一个接口和封装有关如何调用原始方法的必要信息。

The Command pattern is basically a work-around for the fact that java does not treat methods (functions) as first class objects and it's thus impossible to pass them around. Instead, you create an interface that can be passed around and that encapsulates the necessary information about how to call the original method.

因此​​,对于你的例子:

So for your example:

 interface Command {
     public void execute();
 }

和你再通过该命令的一个实例,当你执行登录()函数(未经测试,我总是忘了怎么去匿名类右):

and you then pass in an instance of this command when you execute the login() function (untested, I always forget how to get anonymous classes right):

 final GUI target = this;
 command = new Command() {
     @Override
     public void execute() {
         target.notifyUser();
     }
 };
 mCommunicationManager.login(command);

而在login()函数(管理器保存参考命令):

And in the login() function (manager saves reference to command):

public void login() {
    command.execute();
}

编辑:
我也许应该提及的是,虽然这是它是如何工作的一般解释,在Java中已经有一些管道用于此目的,即的ActionListener 和相关类(的actionPerformed()基本的execute()命令)。这些大多是旨在与AWT和/或Swing类虽然使用的,并因此具有特定于该用例特征

edit: I should probably mention that, while this is the general explanation of how it works, in Java there is already some plumbing for this purpose, namely the ActionListener and related classes (actionPerformed() is basically the execute() in Command). These are mostly intended to be used with the AWT and/or Swing classes though, and thus have features specific to that use case.

这篇关于如何实现在Java中回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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