title: How to implement builder pattern with fluent-interface with immutable results and extended interfaces?
body: <p>I would like to implement a builder with a fluent interface.</p>
  
  <h1>Requirements</h1>
  
  <p>To make things more difficult there are two additional requirements:</p>
  
  <ol>
  <li><p>I would like the returned objects to be immutable so that it can be used as follows and the interface should be extendible in derived interfaces:</p>
  
  <pre><code>ConcreteBuilder b1 = builder().setValue(1);
  ConcreteBuilder b2 = b1.setValue(2);
  
  ComplexObject o1 = b1.build();
  o1.getValue(); // should return 1
  
  ComplexObject o2 = b2.build();
  o2.getValue(); // should return 2
  </code></pre>
  
  <p>First an issue open for discussion: should the builder be immutable so that it has above semantics? (it's briefly commented on by <a href="https://stackoverflow.com/users/157852/jonathan">Jonathan</a> in <a href="https://stackoverflow.com/q/5396534/814206">How to create an immutable builder of an immutable class that contains a set?</a>)</p></li>
  <li><p>The builder interface should be extendible:</p>
  
  <pre><code>interface Builder&lt;T extends Builder&lt;T&gt;&gt; {
       T setValue(int v);
  }
  
  interfacte BuilderEx&lt;T extends BuilderEx&lt;T&gt;&gt; {
       T someOtherOpp();
  }
  </code></pre>
  
  <p>(it uses self referencing generics as described by Eamonn McManus<sup><a href="http://weblogs.java.net/node/642849" rel="nofollow noreferrer" title="Eamonn McManus - Using the builder pattern with subclasses">1</a></sup>)</p></li>
  </ol>
  
  <h1>Implementation</h1>
  
  <p>To satisfy the first requirement AbstractBuilder's implementation is similar to the one in <a href="https://stackoverflow.com/q/5396534/814206">How to create an immutable builder of an immutable class that contains a set?</a> :</p>
  
  <pre><code>class AbstractBuilder&lt;T extends Builder&lt;T&gt;&gt; implements Builder&lt;T&gt; {
      T setValue(final int v) {
          return castToConcrete(new AbstractBuilder&lt;T&gt;() {
              // with build() overridden to use v
          }
      }
  }
  </code></pre>
  
  <p><code>castToConcrete</code> should be similar to <code>self()</code><sup><a href="http://weblogs.java.net/node/642849" rel="nofollow noreferrer" title="Eamonn McManus - Using the builder pattern with subclasses">1</a></sup> and <code>getThis()</code><sup><a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206" rel="nofollow noreferrer" title="Angelika Langer - Java generics FAQs:What is the &#39;getThis&#39; trick?">2</a></sup>: converting a <code>Builder&lt;T&gt;</code> to <code>ConcreteBuilder</code> or <code>ConcreteBuilderEx</code> depending on <code>T</code> . The problem is how to implement <code>castToConcrete</code></p>
  
  <p>Since the builder methods create new instances the method of overriding <code>getThis</code> in <code>ConcreteBuilder</code> doesn't work.  Using a "functor"<sup><a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Function.html" rel="nofollow noreferrer" title="An example implementation of Functor: Guava - Function">3</a></sup> supplied to the constructor of <code>AbstractBuilder</code> and stored in a field enables conversion of <code>Builder&lt;T&gt;</code> to <code>T</code>.  Add the following field (it uses the Guava library<sup><a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Function.html" rel="nofollow noreferrer" title="An example implementation of Functor: Guava - Function">3</a></sup> to <code>AbstractBuilder</code>:</p>
  
  <pre><code>Function&lt;Builder&lt;T&gt;, T&gt; castToConcrete;
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>My current implementation of <code>castToConcrete</code> turns quite ugly:</p>
  
  <ol>
  <li>it's an <code>if-else</code> tree on <code>instanceof</code></li>
  <li>when <code>input instanceof Builder</code> and not <code>input instanceof BuilderEx</code> a wrapper class which needs to define all methods in <code>BuilderEx</code>, forward the ones in <code>Builder</code> to <code>input</code> and perform a default or nil operation for the others.</li>
  <li><strike>when <code>input instanceof BuilderEx</code> it casts input to <code>ConcreteBuilderEx</code> even though <code>input</code> is probably not an actual instance of <code>ConcreteBuilderEx</code> but an instance of an annonymous subclass of <code>AbstractBuilderEx</code> (hoping that this works.)</strike>[edit by OP]Doesn't work: we need a wrapper for every interface[/edit]</li>
  </ol>
  
  <p>What is a better/cleaner way to do this?</p>