简体   繁体   中英

Service Provider Interface without the Provider

I am reading Bloch's Effective java book [1] and came across the following example of SPI:

//Service interface
public interface Service {
  //Service specific methods here
}

//Service provider interface
public interface Provider {
  Service newService();
}

//Class for service registration and access
public class Services {
  private Services(){}

  private static final Map<String, Provider> providers =
    new ConcurrentHashMap<String, Provider>();
  public static final String DEFAULT_PROVIDER_NAME = "<def>";

  //Registration
  public static void registerDefaultProvider(Provider p) {
    registerProvider(DEFAULT_PROVIDER_NAME, p);
  }
  public static void registerProvider(String name, Provider p) {
    providers.put(name, p);
  }

  //Access
  public static Service newInstance() {
    return newInstance(DEFAULT_PROVIDER_NAME);
  }
  public static Service newInstance(String name) {
     // you get the point..lookup in the map the provider by name
     // and return provider.newService();
  }

This my question: why is the Provider interface necessary? Couldn't we have just as easily registered the Service(s) themselves - eg maintain a map of the Service implementations and then return the instance when looked up? Why the extra layer of abstraction?

Perhaps this example is just too generic - any "better" example to illustrate the point would be great too.


[1] Second edition , Chapter 2. The first edition example does not refer to the Service Provider Interfaces.

Why is the Provider interface necessary? Couldn't we have just as easily registered the Service(s) themselves - eg maintain a map of the Service implementations and then return the instance when looked up?

As others have stated, the purpose of a Provider is to have an AbstractFactory that can make Service instances. You don't always want to keep a reference to all the Service implementations because they might be short lived and/or might not be reusable after they have been executed.

But what is the purpose of the provider and how can you use a "provider registration API" if you don't have a provider

One of the most powerful reasons to have a Provider interface is so you DON'T need to have an implementation at compile time. Users of your API can add their own implementations later.

Let's use JDBC as an example like Ajay used in another answer but let's take it a little further:

There are many different types of Databases and database vendors who all have slightly different ways of managing and implementing databases (and perhaps how to query them). The creators of Java can't possibly create implementations of all these different possible ways for many reasons:

  • When Java was first written, many of these database companies or systems didn't exist yet.
  • Not all these database vendors are open source so the creators of Java couldn't know how to communicate with them even if they wanted to.
  • Users might want to write their own custom database

So how do you solve this? By using a Service Provider .

  • The Driver interface is the Provider . It provides methods for interacting with a particular vendor's databases. One of the methods in Driver is a factory method to make a Connection instance(which is the Service ) to the database given a url and other properties (like user name and password etc).

Each Database vendor writes their own Driver implementation for how to communicate with their own database system. These aren't included in the JDK; you must go to the company websites or some other code repository and download them as a separate jar.

To use these drivers, you must add the jar to your classpath and then use the JDK DriverManager class to register the driver.

The DriverManager class has a method registerDriver(Driver) that is used to register a Driver instance in the Service Registration so it can be used. By convention, most Driver implementations register at class loading time so all you have to do in your code is write

Class.forname("foo.bar.Driver"); 

To register the Driver for vendor "foo.bar" (assuming you have the jar with that class in your classpath.)

Once the Database Drivers are registered, you can get a Service implementation instance that is connected to your database.

For example, if you had a mysql database on your local machine named "test" and you had a user account with username "monty" and password "greatsqldb" then you can create a Service implementation like this :

Connection conn =
   DriverManager.getConnection("jdbc:mysql://localhost/test?" +
                               "user=monty&password=greatsqldb");

The DriverManager class sees the String you passed in and finds the registered driver that can understand what that means. (This is actually done using the Chain of Responsibility Pattern by going through all the registered Drivers and invoking their Driver.acceptsUrl(Stirng) method until the url gets accepted)

Notice that there is no mysql specific code in the JDK. All you had to do is register a Driver of some vendor and then pass a properly formatted String to the Service Provider. If we later decide to use a different database vendor (like oracle or sybase) then we just swap jars and modify the our connection string. The code in the DriverManager does not change.

Why didn't we just make a connection once and keep it around? Why do we need the Service?

We might want connect/disconnect after each operation. Or we might want to keep the connection around longer. Having the Service allows us to create new connections whenever we want and does not preclude us from keeping a reference to it to re-use later.

This is a very powerful concept and is used by frameworks to allow many possible permutations and extensions without cluttering the core codebase.

EDIT

Working with multiple Providers and Providers that provide multiple Services :

There is nothing stopping you from having multiple Providers. You can connect to multiple databases created using different database vendor software at the same time. You can also connect to multiple databases produced by the same vendor at the same time.

Multiple services - Some Providers may even provide different Service implementations depending on the connect url. For example, H2 can create both file system based or in-memeory based databases. The way to tell H2 which one you want to use is a different url format. I haven't looked at the H2 code, but I assume the file based and the in memory based are different Service implementations.

Why doesn't the DriverManager just manage Connections and Oracle could implement the OracleConnectionWrapper? No providers!

That would also require you to know that you have an Oracle connection. That is very tight coupling and I would have to change lots of code if I ever changed vendors.

The Service Registration just takes a String. Remember that it uses the chain of Responsiblity to find the first registered Provider that knows how to handle the url. the application can be vendor neutral, and it can get the connection url and Driver class name from a property file. That way I don't have to recompile my code if I change vendors. However, if I hardcoded refernences to "OracleConnectionWrapper" and then I changed vendors, I would have to rewrite portions of my code and then recompile.

There is nothing preventing someone from supporting multiple database vendor url formats if they want. So I can make a GenericDriver that could handle mysql and oracle if I wanted to.

If you might need more than one service of each type, you can't just reuse the old Services. (Additionally, tests and the like might want to create fresh services for each test, rather than reusing services that might have been modified or updated by previous tests.)

I think the answer is mentioned in Effective Java along with an example.

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 .

So as you have correctly noted it is not a must to have the Provider interface but just a little cleaner approach.

So seems like you can have multiple Provider s for the same Service and based on a specific Provider name you may get different instances of the same Service. So I would say each Provider is kind of like factory that creates the service appropriately.

For example suppose class PaymentService implements Service and it requires a Gateway . You have PayPal and Chase gateway that deal with those payment processors. Now you create a PayPalProvider and ChaseProvider each of which knows how to create the correct the PaymentService instance with the right gateway.

But I agree, seems contrived.

As a synthesis of the other answers (the fourth component is the textual reason) I think this is to limit compilation dependencies. With the SPI, you have all the tools to exclude en explicit reference to the implementation:

  • The META-INF/services/ directory contains files mentioning the available service provider implementations
  • The ServiceLoader standard class allows the resolution of the available implementations names and by the way a dynamic construction [1] .

The SPI was not mentioned in the first edition. It was perhaps not the right place to include it in an item about static factories. The DriverManager mentioned in the text is a hint, but Bloch does not go in deep. In a way, the platform implements a kind of ServiceLocator pattern to reduce compilation dependencies, depending on the environment. With a SPI in your abstract factory, it becomes the ServiceFactory of a ServiceLocator with the help of the ServiceLoader for modularity.

The ServiceLoader iterator could be used to populate dynamically the services map of the example.


[1] In an OSGi environment, this is a subtle operation .

Service Provider Interface without a provider

Let's see how it would look like without a provider.

//Service interface
public interface Service {
  //Service specific methods here
}

//Class for service registration and access
public class Services {
  private Services(){}

  private static final Map<String, Service> services =
    new ConcurrentHashMap<String, Service>();
  public static final String DEFAULT_SERVICE_NAME = "<def>";

  //Registration
  public static void registerDefaultService(Provider p) {
    registerService(DEFAULT_SERVICE_NAME, p);
  }
  public static void registerService(String name, Provider p) {
    services.put(name, p);
  }

  //Access
  public static Service getInstance() {
    return newInstance(DEFAULT_SERVICE_NAME);
  }
  public static Service getInstance(String name) {
     // you get the point..lookup in the map the service by name
     // and return it;
  }

As you see, it's possible to create a Service Provider Interface without a Provider interface . Callers of #getInstance(..) eventually wouldn't notice a difference.

Then why do we need a provider?

The Provider interface is an Abstract Factory and Services#newInstance(String) is a Factory Method . Both design patterns have the advantage that they decouple service implementation from service registration.

Single responsibility principle

Instead of implementing the service instantiation in a startup event handler, which registers all services, you create one provider per service. This makes it loosely coupled and easier to refactor, because Service and Service Provider could be put near to each other, for example into another JAR-file.

"Factory methods are common in toolkits and frameworks, where library code needs to create objects of types that may be subclassed by applications using the framework." [1]

Lifetime management :

You might have realized in the upper code without providers, that we're registering service instances instead of a provider, which could decide to instantiate a new service instance.

This approach has some disadvantages:

1. Service instances have to be created before the first service call. Lazy initialization isn't possible. This will delay startup and bind resources to services which are rarely used or even never.

1b. You "cannot" close services after usage, because there is no way to reinstantiate them. (With a provider you could design the service interface in a way that the caller has to call #close() , which informs the provider and the provider decides to keep or finalize the service instance.)

2. All callers will use the same service instance, therefore you have to make sure that it's thread-safe. But making it thread-safe will make it slow. In contrary a provider might choose to create a couple of service instances to reduce retention time.

Conclusion

A provider interface isn't required, but it encapsulates service-specific instantiation logic and optimizes resource allocation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM