2016-11-28 1 views
1

나는 다음과 같은 공장변환 공장 패턴

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 >() {})와 같은 바인딩을 사용하고 있습니다. ((과일 ) FruitFactory.get Apple.class, 새로운 TypeOfFruits())); – Schumi

+0

정말 그 이상을 도울 수 있을지 모르겠다. 질문을 명확하게하여 좀 더 명확하게 해줄 수 있을까? 그동안 Guice 사용자 가이드 (github.com/google/guice/wiki/Motivation)를 확인하십시오. 당신의 Guice 솔루션은 최소한 그들의 예제 중 하나처럼 보일 것입니다. –