2012-06-19 36 views
0

在GWT中使用java泛型是否對GWT編譯器有任何好處?這是否有助於創建更小或更高效的JavaScript代碼,或者它是否與在Java中使用它們具有相同的好處。在GWT中使用泛型是否有好處

複雜性也帶來使用GWT,MVP與仿製藥。要實現,泛型正確的接口如下所示:

public interface ViewInterface<P extends PresenterInterface<? extends ViewInterface<P>>> { 
} 
public interface PresenterInterface<V extends ViewInterface<? extends PresenterInterface<V>>> { 
} 

請問以上代碼改進JavaScript編譯器結果或者它有沒有效果,如果我剛剛做了如下代碼:

public interface ViewInterface<P extends PresenterInterface<?>> { 
} 
public interface PresenterInterface<V extends ViewInterface<?>> { 
}    

如果存在的性能沒有差別生成的JavaScript的我寧願與第二實施去。 (減樣板)...

希望這是有道理...

回答

1

由於Google I/O 2009 presentation雷瑞恩使用GWT RPC時,提到使用the command pattern design。你可以看看演示文稿。有一個圖書館叫做GAD a.k.a GWT Action Dispatcher,其中的想法來自於Rayan在演講中的建議。 GAD由使用泛型的5個組件(類和接口)組成。不使用泛型這將是一個很大的客戶端代碼的類型轉換,以及在所述操作和響應的實現在客戶端和server.The之間共享5個組分i上述服務器代碼:

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 here

換句話說,好處是完全相同的。更少的實例和類型轉換。 希望這有幫助。祝你好運。

+0

感謝@Adio的回覆,我也明白泛型的好處。當我們使用泛型實現MVP時,複雜情況出現在GWT中...我們的界面如下所示: – user1465576

+0

還有gwt-dispatch:http://code.google.com/p/gwt-dispatch/ –

+0

「我們的界面如下所示:「我看不到界面。 – Adelin

相關問題