title: Java - Implementation of a service provider
body: <p>I've the following "pattern" (not really a truly recognized DP but I tend to use it on my solutions a lot) in C# whose aim is to have a central point of object implementation providing based on interfaces (some sort of Factory). </p>
  
  <p>It is accessed as a singleton and users request the implementation of a given interface and are returned the appropriate implementation. </p>
  
  <p>My goal is to to migrate it to Java. I've two prototype solutions but I'm not quite happy with the results since on one of them (the more powerful &amp; complex) I had to put a lot of abstractions on top of the instantiation mechanisms due to Java's generics limitations and the other albeit simpler is also much less powerful (it can't even take advantage of generics - which could be hany in my usage situation).</p>
  
  <p>I'd like to use it in Java the following way to register new implementations:</p>
  
  <p><strong>ServiceFactory.getInstance().addService(IMyInterface.class, new MyImplementation());</strong></p>
  
  <p>(Where MyImplementations implements IMyInterface).</p>
  
  <p>Follows the original C# version and the two Java versions:</p>
  
  <p><strong>C# Code</strong></p>
  
  <pre><code>public class ObjectProvider&lt;ObjectType, BaseObjectType&gt; : IObjectProvider&lt;BaseObjectType&gt;
     where ObjectType : BaseObjectType, new()
  {
  
    public BaseObjectType ObjectInstance
    {
      get { return (BaseObjectType)new ObjectType(); }
    }
  }
  
  public class ServiceManager
  {
    private static Dictionary&lt;Type, object&gt; _providersList = null;
    private static object _listLocker = new object();
  
    private ServiceManager() { }
  
    private static void InicializeProvidersList()
    {
      _providersList = new Dictionary&lt;Type, object&gt;();
  
      _providersList.Add(typeof(IMyType), new ObjectProvider&lt;MyImplementation, IMyType&gt;());
         ...
     }
  
     private static Dictionary&lt;Type, object&gt; ProvidersList
     {
       get
          {
              lock (_listLocker)
              {
                  if (_providersList == null)
                      InicializeProvidersList();
                  return _providersList;
              }
          }
      }
  
      public static BusinessType GetBusinessClass&lt;BusinessType&gt;()
      {
          Dictionary&lt;Type, object&gt; list = ProvidersList;
          Type pretendedType = typeof(BusinessType);
          if (!list.ContainsKey(pretendedType))
              return default(BusinessType);
          IObjectProvider&lt;BusinessType&gt; provider = list[pretendedType] as  IObjectProvider&lt;BusinessType&gt;;
          return provider.ObjectInstance;
      }
  }
  </code></pre>
  
  <p><strong>Java (More Powerful Solution) Code</strong></p>
  
  <pre><code>public interface IInstantiator 
  {
    &lt;BaseObjectType&gt; BaseObjectType getInstance(Object... args);
    void setCacheable(boolean value);
  }
  
  public abstract class BaseInstantiator &lt;BaseObjectType, ObjectType extends BaseObjectType&gt;  implements IInstantiator
  {
     protected Class&lt;ObjectType&gt; objectType; 
     protected boolean isCacheable = true;
     protected BaseObjectType cache;
  
     public BaseInstantiator(Class&lt;ObjectType&gt; objectType)
     {
       this.objectType = objectType;
     }
  
     public void setCacheable(boolean value)
     {
       this.isCacheable = value;
     }
  
     @SuppressWarnings("unchecked")
     public final BaseObjectType getInstance(Object... args)
     {
       if(isCacheable &amp;&amp; cache != null)
       {
         return cache;
       }
       else
       {
          BaseObjectType objectType = createInstance(args);
  
          if(isCacheable)
          {
            cache = objectType;
          }
  
          return objectType;
       }
    }
  
    protected abstract BaseObjectType createInstance(Object... args);
  }
  
  public class Instantiator &lt;BaseObjectType, ObjectType extends BaseObjectType&gt; extends      BaseInstantiator &lt;BaseObjectType, ObjectType&gt; 
  {
  
    public Instantiator(Class&lt;ObjectType&gt; ot)
    {
      super(ot);
    }
  
    @Override
    protected BaseObjectType createInstance(Object... args)
    {
       try 
       {
         return objectType.newInstance();
       }
       catch (InstantiationException e) 
       {
          e.printStackTrace();
       }
       catch (IllegalAccessException e) 
       {
          e.printStackTrace();
       }
  
       return null;
    }
  }
  
  
  public class ServiceFactory 
  {
    private HashMap&lt;Class, IInstantiator&gt; services;
    private static ServiceFactory instance;
  
    public &lt;BaseObjectType&gt; void addService(Class&lt;BaseObjectType&gt; baseObjectType, IInstantiator instantiator)
     {
       this.getServices().put(baseObjectType, instantiator);
     }
  
     @SuppressWarnings("unchecked")
     public &lt;BaseObjectType&gt; BaseObjectType getService(Class&lt;BaseObjectType&gt; baseObjectType, Object... args)
     {
  
       if(! getServices().containsKey(baseObjectType))
       {
          throw new NoSuchElementException("Unknown service interface!");
       }
       else
       {
         try 
         {
            return (BaseObjectType) getServices().get(baseObjectType).getInstance(args);
         } 
         catch (Exception e) 
         {
            return null;
         }
       }
     }
  
     private ServiceFactory () { }
  
     public static synchronized ServiceFactory getInstance()
     {
  
       if(ServiceFactory.instance == null)
       {
          ServiceFactory.instance = new ServiceFactory();
          populate();
       }
  
       return ServiceFactory.instance;
     }
  
     private static void populate()
     {
        //...
     }
  
     private HashMap&lt;Class, IInstantiator&gt; getServices() 
     {
  
       if(this.services == null)
       {
         this.services = new HashMap&lt;Class, IInstantiator&gt;();
       }
  
       return this.services;
     }
  }
  </code></pre>
  
  <p><strong>Java (More Simple and Less Powerful Solution)</strong></p>
  
  <pre><code>@SuppressWarnings("rawtypes")
  public class ManagerFactory
  {
    private Map&lt;Class, Object&gt; managers;
  
    private ManagerFactory()
    {
      populateFactory();
    }
  
    private static class SingletonHolder
    {
      public static final ManagerFactory INSTANCE = new ManagerFactory();
    }
  
    public static ManagerFactory getInstance()
    {
      return SingletonHolder.INSTANCE;
    }
  
    private void populateFactory()
    {
      this.managers = new HashMap&lt;Class, Object&gt;();
  
      this.managers.put(ITaskManager.class, new TaskManager());
    }
  
    public Object getManager(Class interfaceClass)
    {
      if(this.managers.containsKey(interfaceClass))
        return this.managers.get(interfaceClass);
      else 
        return null;
    }
  }
  </code></pre>
  
  <p>Can anyone offer some help on this one?</p>
  
  <p>Thanks in advance!</p>