2017-09-19 6 views
0

私はGWT.createで自分のクラスのパラメトリックコンストラクタを使う必要がありますが、GWTはデフォルトのコンストラクタを呼び出すだけです。私はGWT.createの呼び出しを新しいClass(引数リスト)の呼び出しに置き換えることを考えていました。それは、GWT遅延バインディングでコーディングする正しい方法ですか?GWT.createとnew演算子の代替方法

コードは以下の通りです:ここでは

public interface ProductSelectorMetaFactory extends BeanFactory.MetaFactory { 
     BeanFactory<ProductSelectorTile> getProductSelectorTileFactory(); 
    } 

public CustomTileGrid(DataSource cardViewDataSource, ProductSelectorTile tileType, String fieldState, 
      List<DetailViewerField> list) { 
     setDataSource(cardViewDataSource); 
     setAutoFetchData(true); 
     GWT.create(ProductSelectorMetaFactory.class); 
     setTileConstructor(tileType.getClass().getName()); 
    } 

私はProductSelectorTileクラスのパラメータのコンストラクタを使用します。これをどうすれば使うことができますか?

何か助けていただければ幸いです。 私は現在の問題を説明するのにうまくいかないかもしれませんが、この質問を理解することに疑念があれば自由に感じてください。

+0

ジェネレータは、使用しようとしている場合に備えて廃止予定です(種類)。新しい開発には注釈プロセッサを使用する必要があります。 –

答えて

0

GWT.create()で作成できるパラメータのないヘルパークラスを使用します。そのヘルパークラスは、元のクラスと同じ(パラメータ付き)異なる実装を持つことができます。ヘルパークラスの各実装は元のクラスの適切なインスタンスを作成できます。

ユーザエージェントごとに異なる実装が必要であるとします。 <module>.gwt.xmlではあなたがこれを持っているでしょう(documentationを参照してください):

<replace-with class="<module.path>.MyClassHelperGecko"> 
    <when-type-is class="<module.path>.MyClassHelper" /> 
    <when-property-is name="user.agent" value="gecko1_8" /> 
</replace-with> 

<replace-with class="<module.path>.MyClassHelperSafari"> 
    <when-type-is class="<module.path>.MyClassHelper" /> 
    <when-property-is name="user.agent" value="safari" /> 
</replace-with> 

... 

ヘルパークラスとその実装:基本クラスの

public class MyClassHelper { 
    MyClassInterface getInstance(String param) { 
     return null; 
    }; 
} 

public class MyClassHelperGecko extends MyClassHelper { 
    @Override 
    public MyClassInterface getInstance(String param) { 
     return new MyClassGecko(param); 
    } 
} 

public class MyClassHelperSafari extends MyClassHelper { 
    @Override 
    public MyClassInterface getInstance(String param) { 
     return new MyClassSafari(param); 
    } 
} 

... 

そして最後に実装:

public interface MyClassInterface { 
    // ... 
} 

public class MyClassGecko implements MyClassInterface { 
    public MyClassGecko(String param) { 
     Window.alert("Gecko implementation; param = " + param); 
     // ... 
    } 
} 

public class MyClassSafari implements MyClassInterface { 
    public MyClassSafari(String param) { 
     Window.alert("Safari implementation; param = " + param); 
     // ... 
    } 
} 

... 

は次のようにそれを使用してくださいさまざまなユーザーエージェントのさまざまなアラートが表示されます。

MyClassHelper helper = GWT.create(MyClassHelper.class); 
helper.getInstance("abc"); 

これは一般的なDeferred Binding Using Replacementソリューションです。また、Deferred Binding using Generatorsメソッドもあります。アイデアが得られたら、あなたに最適な方法を選ぶことができます。