Service provider framework随记

本文介绍了服务提供者框架(SPF)的设计模式,包括其四个核心组件:服务接口、注册API、访问API及提供商工厂接口。以JDBC为例,详细解释了各组件的作用,并通过示例代码展示了SPF的基本实现。

Effective java 2

The class of the object returned by a static factory method need not even exist at the time the class containing the method is written. Such flexible static factory methods form the basis of service provider frameworks, such as the java Database Connectivity API (JDBC). A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations.

举了个例子说明静态工厂方法是SPF的基石。又是实现与声明的分离!


There are three essential components of a service provider framework:

 a service interface, which providers implement; 

a provider registration API, which the system uses to register implementations, giving clients access to them;

a service access API, which clients use to obtain an instance of the service.

The service access API typically allows but does not require the client to specify some criteria for choosing a provider. In the absence of such a specifictaion, the API returns an instance of a default implementation. The service access API is the "flexible static factory" that forms the basis of the service provider framework

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.

SPF中的四种角色:service,registration,access,provider factory

In the case of JDBC, Connection plays the part of the service interface, DriverManager.registerDeriver is the provider registration API, DriverManager.getConnection is the service access API, and Driver is the service provider interface.

对于JDBC,各种角色对应关系,很明白。

There are numerous variants of the service provider framework pattern. For example, the service access API can return a richer service interface than the one required of the provider, using the Adapter pattern. Here is a simple implementation with a service provider interface and a default provider:

//Service provider framework sketch

//Service interface
public interface Service{
   ...//Service-specific methods go there
}

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

//Noninstantiable class for service registration and access
public class Services{
  private service() {} //Prevents instantiation 
// 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();
}
}

今天先写到这里吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值