简体   繁体   English

对精炼工厂使用通用通用默认实现

[英]Usage of a common generic default implementation for refined Factories

I have the following problem: 我有以下问题:

There is a generic Factory interface 有一个通用的Factory接口

interface Factory<T> {
  T create();
}

Now I have two classes T1 and T2, where T2 is a refinement of T1 现在我有两个类T1和T2,其中T2是T1的细化

class T1 {
}

class T2 extends T1 {
}

and two factories for those types: 和这两种类型的工厂:

interface T1Factory extends Factory<T1> {
  public T1 create();
}

interface T2Factory extends Factory<T2> {
  public T2 create();
}

For those factories a default implementation will be provided via Generics: 对于这些工厂,将通过泛型提供默认实现:

class DefaultFactory<T, F extends Factory<T>> implements Factory<T> {
  private F factory;
  ...
  public T create()
  {
    return factory.create();
  }
}

that should be used to implement factories for T1 and T2. 应该用于实现T1和T2的工厂。

class DefaultT1Factory extends DefaultFactory<T1,T1Factory> {
}

class DefaultT2Factory extends DefaultFactory<T2,T2Factory> {
}

Up to here it works fine. 到这里它工作正常。 Now the problem. 现在问题。 Because T2 is a refinement of T1 the Factory for T2 should be usable also as factory for T1. 因为T2是T1的细化,所以T2的工厂也可以用作T1的工厂。

This requires T2Factory to be derived from T1Factory. 这需要T2Factory从T1Factory派生。 If I do this I cannot use the DefaultFactory class to implement the DefaultT2Factory, because T2Factory is no Factory<T2>. 如果我这样做,我不能使用DefaultFactory类来实现DefaultT2Factory,因为T2Factory不是Factory <T2>。 If I add this relationship to the extends clause of T2Factory I got the error that the interface Factory is used more than once. 如果我将此关系添加到T2Factory的extends子句,我得到的错误是接口Factory不止一次被使用。 But I need the Factory interface to be able to use the create method in the default implementation. 但我需要Factory接口才能在默认实现中使用create方法。

From the point of method signatures everything is fine. 从方法签名来看,一切都很好。 As a consequence I've to duplicate the implementation of the default implementation into the to factory implementation. 因此,我要将默认实现的实现复制到工厂实现中。 In my case this coding is quite large and I want to avoid code duplication. 在我的情况下,这个编码非常大,我想避免代码重复。

Any idea how to circumvent this problem. 知道如何规避这个问题。

I think you meant 我想你的意思

Because T2 is a refinement of T1 the Factory for T1 should be usable also as factory for T2. 因为T2是T1的细化,所以T1的工厂也可以用作T2的工厂。 A factory of T1 should not be used as a factory of T2 because T1 IS NOT T2. T1的工厂不应该用作T2的工厂,因为T1不是T2。 However T2 IS T1, so a factory for T2 can be safetly used as a factory of T1(or treated as a factory of T1) 但是T2是T1,因此T2的工厂可以安全地用作T1的工厂(或作为T1的工厂处理)

   public interface T1Factory<J extends T1> extends Factory<T1> {
             public T1 create();
        }

Now when you implement this interface, its cool to say something like 现在当你实现这个界面时,很酷的说出类似的东西

public class Foo implements T1Factory<T2> {
         public T1 create(){
                    .....
          }
 }

Now basically the interface can be parameterized with any type that extends or is T1. 现在基本上可以使用任何扩展或T1的类型来参数化接口。 In this case you stated you wanted to take advantage of the T2 is a T1 relationship. 在这种情况下,你说你想利用T2是一种T1关系。

Hope that helps 希望有所帮助

You can use wildcard to define something like Factory of T or any subtype of T . 您可以使用通配符来定义类似Factory of T或T的任何子类型 If you change signature of DefaultFactory to 如果您将DefaultFactory签名更改为

class DefaultFactory<T, F extends Factory<? extends T>> implements Factory<T> {

Then you should be able to have DefaultFactory<T1, T2Factory> . 然后你应该能够有DefaultFactory<T1, T2Factory>

I think I've found a solution, not really nice, but it seems to work. 我想我找到了一个解决方案,不是很好,但似乎有效。 The answer before is nearly working, but use super instead of extends . 之前的答案几乎正常,但使用super而不是extend The important point is just to be able to reuse the implementation. 重要的是能够重用实现。

This should be possible when relaxing the type constraint for the factory element by using super. 当使用super放宽工厂元素的类型约束时,这应该是可能的。

class DefaultFactory<T, F extends Factory<? super T>> {
  private F factory;

  @SuppressWarnings("unchecked")
  public T create()
  {
    return (T)factory.create();
  }
}

The price is the type unsafetyness when calling the create method, which required an unsafe type cast. 调用create方法时,价格是不安全的类型,这需要一个不安全的类型转换。 But the rest seems to work as expected. 但其余的似乎按预期工作。

T2Factory thereby has to extend T1Factory . T2Factory因此必须扩展T1Factory Then it is unfortunately not a Factory<T2> , but this was required only for being able to call the create method in the default implementation returning the correct type. 然而,遗憾的是它不是Factory <T2> ,但这只是为了能够在默认实现中调用create方法返回正确的类型。 And for this purpose it is sufficient to be a Factory<T1> (which causes the required type cast). 为此目的,它就足以成为Factory <T1> (导致所需的类型转换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM