How to implement callbacks in Java

I have a CommunicationManager class that is responsible for communicating with the server.

It includes the login() and onLoginResponse() methods. If the user logs in, the login() method must be called, and when the server responds, the onLoginResponse() method is executed.

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

 mCommunicationManager.login(); 

What I do not know how to do is to bind a method from the GUI class to onLoginResponse() . For example, if the GUI class includes a notifyUser() method that displays a message received from the server.

I would really appreciate if someone could show how to bind methods to execute a method from the GUI class (e.g. GUI.notifyUser() ) when an instance of the mCommunicationManager class receives a message from the server and the CommunicationManager.onLoginResponse() method.

Thank!

+10
asynchronous callback
Sep 25 '09 at 8:50
source share
4 answers

Here are two patterns that I see you are using. One is the publication / subscription or observer template mentioned by Pete. I think this is probably what you want, but, seeing that the question mentions the method binding for subsequent execution, I thought I should mention the Command pattern .

The Command pattern is basically a workaround for the fact that java does not treat methods (functions) as objects of the first class, and therefore they cannot be circumvented. Instead, you create an interface that can be passed in, and which encapsulates the necessary information on how to invoke the original method.

So for your example:

  interface Command { public void execute(); } 

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

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

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

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

edit: I should probably mention that although this is a general explanation of how it works, some kind of plumbing already exists in Java for this purpose, namely the ActionListener and related classes ( actionPerformed() basically execute() in Command ) They are mainly intended for use with the AWT and / or Swing classes, and therefore have functions specific to this use case.

+25
Sep 25 '09 at 13:16
source share

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

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

+3
Sep 25 '09 at 8:54
source share

You can see the swt fragments (look at the listeners)

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

or you use the runnable class, replacing the run method with your callback code when creating the instance

+1
Sep 25 '09 at 8:53
source share

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

You may need to implement this through interfaces (for example, as a command listener.).

Perhaps this site will be useful:

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

+1
Sep 25 '09 at 8:53
source share



All Articles