正如Google I/O 2009 演示文稿Ray Ryan 提到的,在使用 GWT RPC 时使用命令模式设计。您可以查看演示文稿。有一个名为 GAD aka GWT Action Dispatcher 的库,其中的想法来自演示文稿中 Rayan 的推荐。GAD 由 5 个使用泛型的组件(类和接口)组成。如果没有泛型,在客户端代码以及在客户端和服务器之间共享 Actions 和 Responses 实现的服务器代码中将会有很多类型转换。我上面提到的 5 个组件是:
1-
public interface Action<T extends Response> extends Serializable {
}
2-
public interface ActionHandler<K extends Action, T extends Response> {
/**
* Handles the provided action and retuns response of it.
*
* @param action the action to be handled
* @return the response to be returned
*/
T handle(K action);
}
3-
public interface ActionDispatcher {
/**
* Dispatches the provided action to a proper handler that is responsible for handling of that action.
* <p/> To may dispatch the incomming action a proper handler needs to be bound
* to the {@link com.evo.gad.dispatch.ActionHandlerRepository} to may the dispatch method dispatch the
* incomming request to it.
*
* @param action the action to be handled
* @param <T> a generic response type
* @return response from the provided execution
* @throws ActionHandlerNotBoundException is thrown in cases where no handler has been bound to handle that action
*/
<T extends Response> T dispatch(Action<T> action) throws ActionHandlerNotBoundException;
}
4-
public interface ActionHandlerRepository {
ActionHandler getActionHandler(Class<? extends Action> aClass);
}
当一个动作被传递给动作调度器时,动作调度器调用 ActionHandlerRepository 并要求它获取正确的 ActionHandler 然后调用方法句柄。你可以在这里找到 GAD 。
换句话说,好处是完全一样的。更少的 instanceof 和类型转换。希望这会有所帮助。祝你好运。