加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Can Guice可以根据参数自动创建不同类的实例吗?

发布时间:2020-12-15 02:55:29 所属栏目:Java 来源:网络整理
导读:标准对象工厂可能如下所示: interface I { ... }class A implements I { ... }class B implements I { ... }class IFactory { I getI(int i) { switch (i) { case 1: return new A(); default: return new B(); } }} 是否可以设置绑定以便为我完成切换,即我
标准对象工厂可能如下所示:
interface I { ... }
class A implements I { ... }
class B implements I { ... }

class IFactory {
    I getI(int i) {
        switch (i) {
        case 1: return new A();
        default: return new B();
        }
    }
}

是否可以设置绑定以便为我完成切换,即我所做的只是调用getInstance或者注入?我正在寻找辅助注射,但这似乎是不同的主题:https://code.google.com/p/google-guice/wiki/AssistedInject

解决方法

听起来你正在寻找 MapBinder,这是 Multibindings功能的一部分.请注意,您仍然需要放入某种IFactory或其他工厂接口,因为getInstance不像getI那样接受参数,并且您仍然需要在某处建立从整数到类实现的映射.

MapBinder式

class IModule extends AbstractModule {
  @Override public void configure() {
    MapBinder<Integer,I> myBinder =
        MapBinder.newMapBinder(binder(),Integer.class,I.class);
    myBinder.addBinding(1).to(A.class);
    // Add more here.
  }
}

// You can even split the MapBinding across Modules,if you'd like.
class SomeOtherModule extends AbstractModule {
  @Override public void configure() {
    // MapBinder.newMapBinder does not complain about duplicate bindings
    // as long as the keys are different.
    MapBinder<Integer,I.class);
    myBinder.addBinding(3).to(C.class);
    myBinder.addBinding(4).to(D.class);
  }
}

配置有这些模块的注射器将提供可注射的Map< Integer,I>.有一切事物的实例;这里它将是一个从3到完全注入的A实例的三条目映射,从3到C实例,从4到D实例.这实际上是对您的switch示例的改进,它使用了new关键字,因此没有将任何依赖项注入A或B.

对于不会创建如此多浪费实例的更好选项,请注入Map< Integer,Provider< I>> MapBinder也自动提供.像这样使用它:

class YourConsumer {
  @Inject Map<Integer,Provider<I>> iMap;

  public void yourMethod(int iIndex) {
    // get an I implementor
    I i = iMap.get(iIndex).get();
    // ...
  }
}

但是,要提供“默认”实现(和不透明的接口),您需要在MapBinder地图之上实现自己的短包装器:

class IFactory {
  @Inject Map<Integer,Provider<I>> iMap;
  @Inject Provider<B> defaultI; // Bound automatically for every Guice key

  I getI(int i) {
    return iMap.containsKey(i) ? iMap.get(i).get() : defaultI.get();
  }
}

更简单,工厂风格

如果上面看起来有点矫枉过正,请记住你可以inject an Injector并创建一个从键到实现的本地Map. (你也可以像我在这里一样使用ImmutableMap).

class IFactory {
  @Inject Injector injector; // This is a bad idea,except for times like this
  @Inject Provider<B> defaultI;
  static final ImmutableMap<Integer,Class<? extends I>> map = ImmutableMap.of(
      1,A.class,3,C.class,4,D.class);

  I getI(int i) {
    return map.containsKey(i)
        ? injector.getInstance(map.get(i))
        : defaultI.get();
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读