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!
To implement a callback functionCreate the managed callback function. The example declares a delegate type, called CallBack , which takes two arguments (hwnd and lparam). The first argument is a handle to the window; the second argument is application-defined. In this release, both arguments must be integers.
In the case of Event-driven programming, we pass a reference to a function which will get called when an event occurs. This mechanism is termed as a callback. Java does not support function pointers. So we can not implement the same direction. But using interfaces we can achieve the same very easily.
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
A callback function executes when an asynchronous operation completes. Here is an example of how a setTimeout function works: function printMe() { console.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With