【问题标题】:Guice - how to implement a factory that returns different implementationsGuice - 如何实现一个返回不同实现的工厂
【发布时间】:2014-08-10 08:35:23
【问题描述】:

假设我有一个名为 Guice 服务的服务,这是它的构造函数

public GuiceService(IPayment payment) {
    this.payment = payment;
}

我的代码曾经使用枚举来创建它

IPayment payment = new PaymentFactory.create(PaymentType.Cash);
NaiveService naiveService = new NaiveService(payment);

而且我必须在某个地方进行工厂实现。像这样的

public IPayment create(PaymentType paymentType) {
    IPayment cardPayment = null;
    switch (paymentType) {
        case Cash:
            cardPayment = new CashPayment(100);
            break;
        case Card:
            cardPayment = new CardPayment(10, 100);
            break;
    }
    return cardPayment;

现在我想使用 Guice,我想我想使用 FactoryModuleBuilder。

  1. 如果我有多个 IPayment 的实现,该怎么做。
    (例如 CardPayment、CashPayment)
    这适用于一个

    install(new FactoryModuleBuilder()
       .implement(IPayment.class, CashPayment.class)
       .build(IPaymentFactory.class));
    
  2. 如何实现构造函数?
    它还会获得 IPayment 吗?还是会得到 Guice 创建的 factoryImpl?

谢谢

【问题讨论】:

    标签: java guice factory


    【解决方案1】:

    你现有的实现是你能得到的最好的。

    为了清楚起见,让我们写出一个通用 IPaymentFactory:

    public interface IPaymentFactory {
      IPayment create(/* ... */);
    }
    

    所以 IPaymentFactory 的实例定义了一个方法,它接受一些参数并返回一个 IPayment 的实例。您可以自己编写一个实现,显然您已经完成了,但 Guice 的 FactoryModuleBuilder 会自动提供类似这样的接口实现。您无需为该类定义任何其他内容:Guice 将为您连接构造函数,并将其绑定到 IPaymentFactory 以便您可以注入 IPaymentFactory 实例,使用您的参数调用 create(...),并获取 IPayment 实例。

    看起来你想要的是一个需要枚举的工厂:

    public interface IPaymentFactory {
      IPayment create(PaymentType paymentType);
    }
    

    ...但是考虑到 CashPayment 采用一个任意参数,而 CardPayment 采用两个任意参数,并且考虑到它们之间的选择需要映射到任意 PaymentType 枚举,您几乎没有给出 Guice em> 有足够的信息来构造正确的对象。

    Guice FactoryModuleBuilder 的设计更多是为了将构造函数参数与依赖项结合起来:

    // Constructor:
    @Inject public BitcoinPayment(
        @Assisted long value,         // varies by instance as a constructor parameter
        BitcoinService bitcoinService // passed-in dependency satisfied by Guice
        ) { /* ... */ }
    
    // Factory interface:
    public IBitcoinPaymentFactory {
      BitcoinPayment create(long value); // users don't need to know about dependencies!
    }
    
    // Factory binding...
    install(new FactoryModuleBuilder().build(IBitcoinPaymentFactory.class));
    
    // ...which lets Guice write the equivalent of:
    public GeneratedBitcoinPaymentFactory implements IBitcoinPaymentFactory {
      @Inject Provider<BitcoinService> bitcoinServiceProvider;
    
      @Override public BitcoinPayment create(long value) {
        return new BitcoinPayment(value, bitcoinServiceProvider.get());
      }
    }
    

    一方面,工厂比你想象的要笨:它只是将参数与依赖项结合起来得到一个完整的列表。另一方面,它很方便:您只需指定一次依赖列表,其余的由 Guice 完成。

    总而言之: FactoryModuleBuilder 不会解决您的问题,但它可以帮助您为 CashPayment 和 CardPayment 创建工厂,然后您可以将其注入到您的手动 PaymentFactory 实现中(仍然需要存在以某种形式)。

    附:在您的示例中,这可能是演示的“玩具问题”,您可能不需要使用 Guice。对于需要依赖的服务对象,Guice 是一个很好的解决方案,但是数据对象(如支付)或其他似乎不需要依赖的对象(如 GuiceService 或 NaiveService)可以直接使用构造函数来构造。一旦他们开始需要 Guice 注入的依赖项,就应该很容易让他们知道 Guice。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      相关资源
      最近更新 更多