プレゼンターとビューにクライアント バンドルと i18n 化されたメッセージが必要になることがよくあります。
それらを取得するための最良の方法はどれですか:インジェクションまたはシングルトン?
解決策 1:これまで、シングルトンを使用してメッセージを取得していました。
public interface MyMessages extends Messages{
String key1();
String key2();
...
class Instance {
private static MyMessages instance = null;
public static MyMessages getInstance() {
if (instance == null) {
instance = GWT.create(MyMessages.class);
}
return instance;
}
}
}
FooView.java :
MyMessages.Instance.getInstance().key1();
解決策 2:このような注射で得た方がよいでしょうか?
private MyMessages i18n;
@Inject
public FooView(MyMessages i18n){
this.i18n=i18n;
}
2番目の解決策は私にはきれいに思えますが、いくつかのi18n文字列を使用する空でないコンストラクターが必要な場合に行き詰まることがあります:
@Inject
private MyMessages i18n;
public Bar(Foo foo){
/*
* do something which absolutely requires i18n here.
* The problem is that injectable attributes are called
* after the constructor so i18n is null here.
*/
foobar();
}