title: Why are class members duplicated in the builder pattern?
body: <p>The builder pattern (in Java) is usually illustrated by an example like this:</p>
  
  <pre><code>public class MyClass {
  
      private String member1;
  
      private String member2;
  
      // Getters &amp; setters
  
      public class MyClassBuilder {
  
          private String nestedMember1;
  
          private String nestedMember2;
  
          public MyClassBuilder withMember1(String member1) {
              this.nestedMember1 = member1;
              return this;
          }
  
          public MyClassBuilder withMember2(String member2) {
              this.nestedMember1 = member1;
              return this;
          }
  
          public MyClass build() {
              MyClass myClass = new MyClass();
  
              myClass.member1 = nestedMember1;
              myClass.member2 = nestedMember2;
  
              return myClass;
          }
  
      }
  
  }
  </code></pre>
  
  <p>So we get two very similar classes and since this pattern is mostly applied when we have a big number of members, we get a big amount of similarities. This violates the DRY principle in my opinion.</p>
  
  <p>What is wrong with the following code?</p>
  
  <pre><code>public class MyClass {
  
      private String member1;
  
      private String member2;
  
      // Getters &amp; setters
  
      public class MyClassBuilder {
  
          private MyClass myClass = new MyClass();
  
          public MyClassBuilder withMember1(String member1) {
              myClass.member1 = member1;
              return this;
          }
  
          public MyClassBuilder withMember2(String member2) {
              myClass.member2 = member1;
              return this;
          }
  
          public MyClass build() {
              return myClass;
          }
  
      }
  
  }
  </code></pre>