title: Java - Purpose of service provider in interface based frameworks?
body: <p>I have listed below notes from 'Effective Java' and the code it references. </p>
  
  <p>My question - </p>
  
  <blockquote>
    <p>why does the author feel service provider interface saves having to
    register by class name and instantiate reflectively ? Can't the
    registration API in the static factory 'providers' be modified to
    register instances of 'services' instead of 'service providers'?</p>
  </blockquote>
  
  <p>So do - </p>
  
  <pre><code>// Maps service names to services
  private static final Map&lt;String, Service&gt; services =
      new ConcurrentHashMap&lt;String, Service&gt;();
  public static final String DEFAULT_PROVIDER_NAME = "&lt;def&gt;";
  
  // Provider registration API
  public static void registerDefaultService( Service s) {
      registerService(DEFAULT_PROVIDER_NAME, s);
  }
  public static void registerService(String name, Service s){
      services.put(name, s);
  }
  </code></pre>
  
  <p>instead of the example from the text below. </p>
  
  <p>Excerpt from the text - </p>
  
  <blockquote>
    <p>An optional fourth component of a service provider framework is a
    service provider interface, which providers implement to create
    instances of their service implementation. In the absence of a service
    provider interface, implementations are registered by class name and
    instantiated reflectively (Item 53). In the case of JDBC, Connection
    plays the part of the service interface, DriverManager.registerDriver
    is the provider registration API, DriverManager.getConnection is the
    service access API, and Driver is the service provider interface.</p>
  </blockquote>
  
  <p>And here's the code - </p>
  
  <pre><code>// Service provider framework sketch
  
  // Service interface
  public interface Service {
      ... // Service-specific methods go here
  }
  
  // Service provider interface
  public interface Provider {
      Service newService();
  }
  
  // Noninstantiable class for service registration and access
  public class Services {
      private Services() { }  // Prevents instantiation (Item 4)
  
      // Maps service names to services
      private static final Map&lt;String, Provider&gt; providers =
          new ConcurrentHashMap&lt;String, Provider&gt;();
      public static final String DEFAULT_PROVIDER_NAME = "&lt;def&gt;";
  
      // Provider registration API
      public static void registerDefaultProvider(Provider p) {
          registerProvider(DEFAULT_PROVIDER_NAME, p);
      }
      public static void registerProvider(String name, Provider p){
          providers.put(name, p);
      }
  
      // Service access API
      public static Service newInstance() {
          return newInstance(DEFAULT_PROVIDER_NAME);
      }
      public static Service newInstance(String name) {
          Provider p = providers.get(name);
          if (p == null)
              throw new IllegalArgumentException(
                  "No provider registered with name: " + name);
          return p.newService();
      }
  }
  </code></pre>
  
  <p>Thanks in advance!!</p>