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

查看:20
本文介绍了如何在 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.

命令模式基本上是解决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();
 }

然后在执行 login() 函数时传入此命令的实例(未经测试,我总是忘记如何正确获取匿名类):

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() 基本上是 Command 中的 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天全站免登陆