How to implement callbacks in Java

前端 未结 4 1269
Happy的楠姐
Happy的楠姐 2020-12-13 11:25

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

It includes methods login() and onLoginResponse()

相关标签:
4条回答
  • 2020-12-13 11:54

    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.

    0 讨论(0)
  • 2020-12-13 12:12

    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

    0 讨论(0)
  • 2020-12-13 12:14

    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.

    0 讨论(0)
  • 2020-12-13 12:15

    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

    0 讨论(0)
提交回复
热议问题