2016-11-28 2 views
1

私は、次の工場コンバートFactoryパターン

public class AppleFactory<T extends Fruit> 
    extends AbstractAppleFactory<Class<T>, TypeOfFruits, Fruits<T>> 
{ 
    private static final AppleFactory factroy = new AppleFactory(); 

    private AppleFactory() 
    { 
    } 

    public static Fruits<? extends Fruit> get(Class<? extends Fruit> clazz, 
     TypeOfFruits typeOfFruits) 
    { 
     return (Fruits<? extends Fruit>) factroy.getInstance(clazz, typeOfFruits); 
    } 

    @Override 
    protected Fruits<T> newInstance(Class<T> clazz, TypeOfFruits typeOfFruits) 
    { 
     final Fruits<T> fruits; 
     fruits = new FruitsImpl<T>(clazz, typeOfFruits,"hello"); 
     return fruits; 
    } 
} 

を持っていると私はこれを行うことによって、Guiceのモジュールのパタパタに変換しようとしています:

@ImplementedBy(AppleFactoryImpl.class) 
public interface AppleFactory<T extends Fruit> 
{ 
    Fruits<? extends Fruit> get(Class<? extends Fruit> clazz, 
     TypeOfFruits typeOfFruits) 
} 

@Singleton 
public class AppleFactoryImpl implements AppleFactory 
{ 
    @Override 
    public Fruits<? extends Fruit> get(Class<? extends Fruit> clazz, 
     TypeOfFruits typeOfFruits) 
    { 
     final Fruits<T> fruits; 
     fruits = new FruitsImpl<T>(clazz, typeOfFruits,"hello"); 
     return fruits; 
    } 
} 

しかし、私はエラーを取得します実装。それはフルーツのためにタイプTを解決できないと言います。

私の最終目標は、すなわち、具体的な実装に

FruitFactory、FruitFactory

を結合し、この工場によって異なる実装を持つことです。これは、プロバイダまたは何か他のものを使用するように変更することができます

、私はアプローチ

に硬すぎないんだけど誰もがこの問題を解決する方法を知っていますか?

+0

最終目標は何ですか? 'FruitFactory ' 'FruitFactory 'を具体的な実装にバインドするのですか? –

+0

@DavidRawson、はい、それは最終目標です。私はこの工場の中に果物を入れて初期化することができるのですか? – Schumi

+0

それは素晴らしいです!これを明確にするために質問を編集できますか?それで誰かが答えることができるでしょう –

答えて

1

あなたが書いたものでは、ジェネリックからコンクリートへの動きはありません。あなたはジェネリック型を最終的にはコンクリーションで解決したいと思いますよね?このようにそれらを結合し

public class AppleFactory implements FruitFactory<Apple> { 

     @Override 
     public Apple get() { 
      return new Apple("apple"); 
     } 
    } 

    public class OrangeFactory implements FruitFactory<Orange> { 

     @Override 
     public Orange get() { 
      return new Orange("orange"); 
     } 
    } 

そして最後モジュール:

汎用インタフェース:

public interface FruitFactory<T extends Fruit> { 
     T get(); 
    } 

具体的な実装で、あなたの目標は、次のようなもので達成することができたのだろうか:

public class FruitFactoryModule implements Module { 

     @Override 
     public void configure(Binder binder) { 
      binder.bind(new TypeLiteral<FruitFactory<Apple>>() {}).to(AppleFactory.class); 
      binder.bind(new TypeLiteral<FruitFactory<Orange>>() {}).to(OrangeFactory.class); 
     } 
    } 
} 
+0

私は上記で提供したファクトリを使用しています。このバインド(new TypeLiteral >(){})のようにバインドしています。((Fruit )FruitFactory.get Apple.class、新しいTypeOfFruits())); – Schumi

+0

私はそれ以上のことを本当に助けることができるかどうかはわかりません。あなたの質問を明確にすることができますか?その間、Guiceユーザーガイド(github.com/google/guice/wiki/Motivation)を確認してください。 Guiceのソリューションは、少なくともその例のように見えるはずです。 –