使用guice在运行时按名称获取实例

时间:2018-05-18 04:10:28

标签: guice

我有以下课程。

Car{}

TwoWheeler exteends Car{}

FourWheeler extends Car{}

RandomClass{
        CarFactory carfactory

        //Inject factory using guice
        public RandomClass(Carfactory carfactory){
          this.carfactory=  carfactory;
        }

        void doSometing(String carType){
          carfactory.getCar(CarType)

        }

  }

  CarFactory{
        Map<String,Car>  carTypeToCarMap;
        Car getCarByType(String type){
                    return carTypeToCarMap.get(type);
     }
}

我可以避免基于carType创建工厂并从guice获取实例吗?

1 个答案:

答案 0 :(得分:0)

@AutoValue
public abstract class Car {
    public abstract Map<String> getCarType();

    public static Car.Builder builder() { return new AutoValue_Car.Builder(); }

    @AutoValue.Builder
    public abstract static class Builder {
        public abstract PatientDto.Builder setCarType(String carType);

        public abstract Car build();
    }
}