简体   繁体   English

Java-基于接口的框架中服务提供者的目的?

[英]Java - Purpose of service provider in interface based frameworks?

I have listed below notes from 'Effective Java' and the code it references. 我在下面列出了“有效Java”及其引用的代码的注释。

My question - 我的问题 -

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'? 不能修改静态工厂“提供者”中的注册API来注册“服务”实例而不是“服务提供者”实例吗?

So do - 也是-

// Maps service names to services
private static final Map<String, Service> services =
    new ConcurrentHashMap<String, Service>();
public static final String DEFAULT_PROVIDER_NAME = "<def>";

// 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);
}

instead of the example from the text below. 而不是下面的文本示例。

Excerpt from the text - 文字摘录-

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). 在没有服务提供者接口的情况下,通过类名注册实现,并以反射方式实例化(项目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. 对于JDBC,Connection充当服务接口的一部分,DriverManager.registerDriver是提供程序注册API,DriverManager.getConnection是服务访问API,而Driver是服务提供程序接口。

And here's the 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<String, Provider> providers =
        new ConcurrentHashMap<String, Provider>();
    public static final String DEFAULT_PROVIDER_NAME = "<def>";

    // 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();
    }
}

Thanks in advance!! 提前致谢!!

The provider in this case is a factory, and all the usual reasons for using a factory apply. 在这种情况下,提供者是工厂,并且使用工厂的所有通常原因都适用。 Different service implementations might need very different construction code, and using a factory means the registry doesn't need to know about the implementation details, just the bare minimum needed to specify the service's behavior. 不同的服务实现可能需要非常不同的构造代码,而使用工厂意味着注册表不需要了解实现细节,只需了解指定服务行为所需的最低要求即可。

我认为服务提供者接口的好处之一可能是,在实际需要时,在服务提供者实例及其使用者之间进行松散耦合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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