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.
wds Sep 25 '09 at 13:16 2009-09-25 13:16
source share