Java服务提供者框架_Effective Java 2.0_Item 1知识点

文章作者:Tyan
博客:noahsnail.com

1. 服务提供者框架介绍

1.1 什么是服务提供者框架

服务提供者框架英文为Service Provider Framework,主要是指:多个服务提供者实现一个服务,系统为客户端提供多个实现,并把他们从多个实现中解耦出来。服务提供者的改变对它们的客户端是透明的,这样提供了更好的可扩展性。例如,JDBC,JMS等就是用了服务提供者框架。

1.2 服务提供者框架的组件

服务提供者框架主要有四个组件:

  • Service Interface:服务接口,将服务通过抽象统一声明,供客户端调用、由各个服务提供者具体实现。

  • Provider Registration API:服务提供者注册API,用于系统注册服务提供者,使得客户端可以访问它实现的服务。

  • Service Access API:服务访问API,用户客户端获取相应的服务。

  • Service Provider Interface:服务提供者接口,这些服务提供者负责创建其服务实现的实例。(非必须)

2. 服务提供者框架实现

Service Interface:

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

Provider Registration API 和 Service Access API:

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

Service Provider Interface:

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

3. 服务提供者架构图

image

参考资料:

1、http://blog.csdn.net/zl3450341/article/details/7227197

2、http://liwenshui322.iteye.com/blog/1267202

3、Effective Java 2.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值