title: Wrapper Factory in Java
body: <p>I may have designed myself into a corner with this problem but I feel like there's a workable known solution to something like this that I'm not seeing. It may well be that I'm completely overcomplicating the problem and skipped over the obvious solution. Any advice would be greatly appreciated.</p>
  
  <p>I have a set of entities defined as interfaces. Each has a concrete implementation and a wrapper implementation. For example:</p>
  
  <pre><code>-- Foo.java
  public interface Foo {
      public String getName();
  }
  
  -- FooImpl.java
  public class FooImpl implements Foo {
      private String name;
  
      public String getName() {
          return name;
      }
  }
  
  -- AbstractWrapper.java
  public class AbstractWrapper {
       protected String decorate(String text) {
           return "** " + text + " **";
       }
  
  -- FooWrapper.java
  public class FooWrapper extends AbstractWrapper implements Foo {
      private Foo wrappedFoo;
  
      public FooWrapper(Foo wrappedFoo) {
           this.wrappedFoo = wrappedFoo;
      }
  
      public String getName() {
           return decorate(wrappedFoo.getName());
      }
  }
  </code></pre>
  
  <p>Now, the part that's complicating the situation is that I'm trying to make a List that automatically wraps the appropriate type with its appropriate wrapper before adding it. Something like:</p>
  
  <pre><code>-- WrappingList.java
  
  /** T is the base entity interface type. */
  public class WrappingList&lt;T&gt; implements List&lt;T&gt; {
      private List&lt;T&gt; wrappedList;
  
      public WrappingList(List&lt;T&gt; wrappedList) {
          this.wrappedList = wrappedList;
      }
  
      ....
  
      public boolean add(T item) {
          return wrappedList.add(wrapItem(item));
      }
  
      protected T wrapItem(T item) {
           T wrappedItem = .....;
           return T;
      }
  }
  </code></pre>
  
  <p>Is there anything I can do to make a somewhat clean factory method here? Or am I already out of luck at this point because of type erasure?</p>