title: Step builder pattern using delegation and enums?
body: <p>I have this project I'm working on and basically this is what I would like to achieve. </p>
  
  <p>This is what I have:</p>
  
  <pre><code>MyObject obj = MyObject.builder()
                         .withValue("string")
                         .withAnotherValue("string")
                         .build();
  
  MyObject obj = MyObject.builder()
                         .withValue("string")
                         .withAnotherValue("string")
                         .withField("key", "value")
                         .build();
  </code></pre>
  
  <p>So the step builder pattern forces the user to use the <code>withValue()</code> method and the <code>withAnotherValue()</code> method in that order. The method <code>field()</code> is optional and can be used as many times as you want.I followed this website for example <a href="http://www.svlada.com/step-builder-pattern/" rel="noreferrer">http://www.svlada.com/step-builder-pattern/</a></p>
  
  <p>So what I would like to achieve is this:</p>
  
  <pre><code>MyObject obj = MyObject.builder(Type.ROCK)
                         .withColour("blue")
                         .withValue("string")
                         .withAnotherValue("string")
                         .build();
  
  MyObject obj = MyObject.builder(Type.STONE)
                         .withWeight("heavy")
                         .withValue("string")
                         .withAnotherValue("string")
                         .withField("key", "value")
                         .build();
  </code></pre>
  
  <p>So in the builder() method you'd put an enum type and based on the enum you'd have a different set of methods appear. So for ROCK the <code>withValue()</code>,<code>withAnotherValue()</code> and <code>withColour()</code> are now mandatory. But for STONE <code>withWeight()</code>, <code>withAnotherValue()</code> and <code>withColour()</code> are mandatory.</p>
  
  <p>I something like this possible? I have been trying for the past two days to figure this out but I just can't seem to get it to give specific methods for each type. It just shows all the methods in the Builder. </p>
  
  <p>Any thoughts and help is much appreciated. </p>
  
  <p>Code:</p>
  
  <p><strong>Enum</strong></p>
  
  <pre><code>public enum Type implements ParameterType&lt;Type&gt; {
  
    ROCK, STONE
  
  }
  </code></pre>
  
  <p><strong>ParameterType</strong></p>
  
  <pre><code>interface ParameterType&lt;T&gt; {}
  </code></pre>
  
  <p><strong>MyObject</strong></p>
  
  <pre><code>public class MyObject implements Serializable {
  
    private static final long serialVersionUID = -4970453769180420689L;
  
    private List&lt;Field&gt; fields = new ArrayList&lt;&gt;();
  
    private MyObject() {
    }
  
    public interface Type {
  
      Value withValue(String value);
    }
  
    public interface Value {
  
      Build withAnotherValue(String anotherValue);
    }
  
    public interface Build {
  
      MyObject build();
    }
  
    public Type builder(Parameter type) {
      return new Builder();
    }
  
    public static class Builder implements Build, Type, Value {
  
      private final List&lt;Field&gt; fields = new ArrayList&lt;&gt;();
  
      @Override
      public Build withAnotherValue(String anotherValue) {
        fields.add(new Field("AnotherValue", anotherValue));
        return this;
      }
  
      @Override
      public Value withValue(String value) {
        fields.add(new Field("Value", value));
        return this;
      }
  
      @Override
      public MyObject build() {
        MyObject myObject = new MyObject();
        myObject.fields.addAll(this.fields);
        return myObject;
      }
    }
  
  }
  </code></pre>