title: Good strategy to avoid duplicate code
body: <p>Lets say i have the following scenario:</p>
  
  <pre><code>public class A {
      public String createString(final String value){
          if (value ==  null){
              throw new NullPointerException("value must NOT be null.");
          }
          final StringBuffer sb = new StringBuffer();
          sb.append("A");
          sb.append("B");
          sb.append("C");
          if (value.length() &gt; 3){
              sb.append("D");
              sb.append("E");
          }
          sb.append("Z");
          return sb.toString();   
      }
  }
  </code></pre>
  
  <p>And another class which should do a similar Task:</p>
  
  <pre><code>public class B {
      public String createString(final String value){
          if (value ==  null){
              throw new NullPointerException("value must NOT be null.");
          }
          final StringBuffer sb = new StringBuffer();
          sb.append("A");
          sb.append("B");
          sb.append("C");
          sb.append("Z");
          return sb.toString();   
      }
  }
  </code></pre>
  
  <p>What would be a good strategy to avoid the duplicate code ? What i came up with so far is that the class B which has a subset functionality of A and therefore should extend from the class A and the same tasks should be refactored into protected methods (assuming they are in the same package). This is what it would look like:</p>
  
  <pre><code>public class A {
      public String createString(final String value){
          final StringBuffer sb = createTheFirstPart(value);
          if (value.length() &gt; 3){
              sb.append("D");
              sb.append("E");
          }
          createTheLastPart(sb);
          return sb.toString();   
      }
  
      protected void createTheLastPart(final StringBuffer sb) {
          sb.append("Z");
      }
  
      protected StringBuffer createTheFirstPart(final String value) {
          if (value ==  null){
              throw new NullPointerException("value must NOT be null.");
          }
          final StringBuffer sb = new StringBuffer();
          sb.append("A");
          sb.append("B");
          sb.append("C");
          return sb;
      }
  }
  </code></pre>
  
  <p>And the class B:</p>
  
  <pre><code>public class B extends A {
      public String createString(final String value){
          final StringBuffer sb = createTheFirstPart(value);
          createTheLastPart(sb);
          return sb.toString();   
      }
  }
  </code></pre>
  
  <p>Another possible solution would be something like this: </p>
  
  <pre><code>public class A {
      public String createString(final String value){
          if (value ==  null){
              throw new NullPointerException("value must NOT be null.");
          }
          final StringBuffer sb = new StringBuffer();
          sb.append("A");
          sb.append("B");
          sb.append("C");
          addSomeSpecialThings(value, sb);
          sb.append("Z");
          return sb.toString();   
      }
  
      protected void addSomeSpecialThings(final String value, final StringBuffer sb) {
          if (value.length() &gt; 3){
              sb.append("D");
              sb.append("E");
          }
      }
  }
  </code></pre>
  
  <p>and class B:</p>
  
  <pre><code>public class B extends A {
      public String createString(final String value){
          return super.createString(value);
      }
  
      protected void addSomeSpecialThings(final String value, final StringBuffer sb) {
          // do nothing
      }
  }
  </code></pre>
  
  <p>Obviously this is not that good because B has an empty impl. of addSomeSpecialThings.
  Also this excample is a very simple one. For Example there could be more difference within the method so it would not be that easy to extract the same functionality.</p>
  
  <p>My solutions are all about inheritance maybe it also would be better to do this with composition. I also thought that this is probably a canidate for the strategy pattern.</p>
  
  <p>Well whats the best approach to such kind of Problem ? Thanks in advance for any help.</p>
  
  <p>kuku.</p>