How to implement callbacks in Java

2019-01-31 14:48发布

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

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.

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

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.

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.

Thanks!

4条回答
ゆ 、 Hurt°
2楼-- · 2019-01-31 15:18

You can look at the swt-snippets (look at the listeners)

http://www.eclipse.org/swt/snippets/

or you use the runnable class , by overwritting the run method with your 'callback'-code when you create an instance

查看更多
够拽才男人
3楼-- · 2019-01-31 15:23

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.

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

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

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

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.

查看更多
成全新的幸福
4楼-- · 2019-01-31 15:32

The idiom used in Java to achieve callback behaviour is Listeners. Construct an interface with methods for the events you want, have a mechanism for registering listener object with the source of the events. When an event occurs, call the corresponding method on each registered listener. This is a common pattern for AWT and Swing events; for a randomly chosen example see FocusListener and the corresponding FocusEvent object.

Note that all the events in Java AWT and Swing inherit ultimately from EventObject, and the convention is to call the listener SomethingListener and the event SomethingEvent. Although you can get away with naming your code whatever you like, it's easier to maintain code which sticks with the conventions of the platform.

查看更多
等我变得足够好
5楼-- · 2019-01-31 15:32

As far as I know Java does not support method binding or delegates like C# does.

You may have to implement this via Interfaces (e.g. like Command listener.).

Maybe this website will be helpful:

http://www.javaworld.com/javaworld/javatips/jw-javatip10.html

查看更多
登录 后发表回答