Template for request-response flow with inner classes

I have an application consisting of two processes, one client process with a SWT based GUI and one server. The client process is very easy, which means that many operations with the graphical interface will have to request the server process or request it for something, for example, in response to a user clicking a button or selecting a menu item. This means that there will be many event handlers that look like this:

// Method invoked e.g. in response to the user choosing a menu item
void execute(Event event) {
    // This code is executed on the client, and now we need some info off the server:
    server.execute(new RemoteRequest() {
        public void run() {
            // This code is executed on the server, and we need to update the client 
            // GUI with current progress
            final Result result = doSomeProcessing();
            client.execute(new RemoteRequest() {
                public void run() {
                    // This code is again executed on the client
                    updateUi(result);
                }
            }
        }
    });
}

, server.execute ( ), ( RemoteRequest ( : , Request , ).

, (, ) , .

- , , .

, , . , Future , ( ), .

, : (, , , )

String personName = nameField.getText();
async exec on server {
    String personAddress = database.find(personName);
    async exec on client {
        addressField.setText(personAddress);
    }
    Order[] orders = database.searchOrderHistory(personName);
    async exec on client {
        orderListViewer.setInput(orders);
    }
}

, , , , . , , , , . , , ...

+5
4

, , . , .

0

Command Pattern AsynchronousCallbacks. GWT , . Serializable, AsyncCallback - .

- :

    // from the client
    server.execute(new GetResultCommand(args), new AsyncCallback<Result>() 
            {
                public void onSuccess(Result result) {
                    updateUi(); // happens on the client
                }
            });

, .

+1

. , (, , ), (, , ).

, :

void execute(Event event) {
    static_execute(server, client, event);
}

// An anonymous class is static if it is defined in a static method. Let use that.
static void static_execute(final TheServer server, final TheClient client, final Event event) {
    server.execute(new RemoteRequest() {
        public void run() {
            final Result result = doSomeProcessing();
            // See note below!
            client.execute(new RemoteRequest() {
                public void run() {
                    updateUi(result);
                }
            });
        }
    });
}

, , , -, , .

- , , , server- > client. : -)

0

: , , . . , instanceof, . . , COMMAND PATTERN - , , , EVENT , STATE PATTERN, .

0

All Articles