title: Usage of a common generic default implementation for refined Factories
body: <p>I have the following problem:</p>
  
  <p>There is a generic Factory interface</p>
  
  <pre><code>interface Factory&lt;T&gt; {
    T create();
  }
  </code></pre>
  
  <p>Now I have two classes T1 and T2, where T2 is a refinement of T1</p>
  
  <pre><code>class T1 {
  }
  
  class T2 extends T1 {
  }
  </code></pre>
  
  <p>and two factories for those types:</p>
  
  <pre><code>interface T1Factory extends Factory&lt;T1&gt; {
    public T1 create();
  }
  
  interface T2Factory extends Factory&lt;T2&gt; {
    public T2 create();
  }
  </code></pre>
  
  <p>For those factories a default implementation will be provided via Generics:</p>
  
  <pre><code>class DefaultFactory&lt;T, F extends Factory&lt;T&gt;&gt; implements Factory&lt;T&gt; {
    private F factory;
    ...
    public T create()
    {
      return factory.create();
    }
  }
  </code></pre>
  
  <p>that should be used to implement factories for T1 and T2.</p>
  
  <pre><code>class DefaultT1Factory extends DefaultFactory&lt;T1,T1Factory&gt; {
  }
  
  class DefaultT2Factory extends DefaultFactory&lt;T2,T2Factory&gt; {
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>This requires T2Factory to be derived from T1Factory. If I do this I cannot use the DefaultFactory class to implement the DefaultT2Factory, because T2Factory is no Factory&lt;T2&gt;.
  If I add this relationship to the extends clause of T2Factory I got the error that the interface Factory is used more than once. But I need the Factory interface to be able to use the create method in the default implementation.</p>
  
  <p>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.</p>
  
  <p>Any idea how to circumvent this problem.</p>