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

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

问题描述

我有一个类CommunicationManager,负责与服务器通信。

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

它包括方法 login() onLoginResponse()。在用户登录的情况下,必须调用方法 login(),当服务器响应方法 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类中, login()方法被简单调用

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()

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.

真正感谢如果任何人可以展示如何绑定方法为了执行方法从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.

谢谢!

推荐答案

这里有两种模式可供您使用。一个是Pete提到的发布/订阅或观察者模式。我认为这可能是你想要的,但看到问题提到绑定一个方法以供以后执行,我想我应该提到命令模式

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.

Command模式基本上是一个解决方法,即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.

因此,对于您的示例:

 interface Command {
     public void execute();
 }

,然后传入此命令的一个实例, c $ c> login() function(未经测试,我总是忘记如何获得匿名类):

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() in Command )。这些主要是为了与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天全站免登陆