SPI 与 API

2016-05-30  本文已影响3411人  faris_shi

背景

Java 中区分 API 和 SPI,通俗的讲:API 和 SPI 都是相对的概念,他们的差别只在语义上,API 直接被应用开发人员使用,SPI 被框架扩展人员使用

理解

API Application Programming Interface

大多数情况下,都是实现方来制定接口并完成对接口的不同实现,调用方仅仅依赖却无权选择不同实现。

SPI Service Provider Interface

而如果是调用方来制定接口,实现方来针对接口来实现不同的实现。调用方来选择自己需要的实现方

More specifically, for Java libraries, what makes them an API and/or SPI?

the API is the description of classes/interfaces/methods/... that you call and use to achieve a goal

the SPI is the description of classes/interfaces/methods/... that you extend and implement to achieve a goal

Put differently, the API tells you what a specific class/method does for you and the SPI tells you what you must do to conform.

从面向接口编程说起

接口调用.jpg

当我们选择在调用方实现方 中间引入 接口。上图没有给出“接口”应该位于哪个“包”中,从纯粹的可能性上考虑,我们有三种选择:

  1. 接口位于实现方所在的包中
  2. 接口位于调用方所在的包中
  3. 接口位于独立的包中

接口位于调用方所在的包中

对于类似这种情况下接口,我们将其称为 SPI, SPI的规则如下:

常见的例子是:插件模式的插件。如:

  1. 数据库驱动 Driver
  2. 日志 Log
  3. dubbo扩展点开发

接口位于实现方所在的包中

对于类似这种情况下的接口,我们将其称作为APIAPI的规则如下:

接口位于独立的包中

如果一个“接口”在一个上下文是API,在另一个上下文是SPI,那么你就可以这么组织

需要注意的事项

SPIAPI 也不一定是接口,我这里都是指狭义的具体的接口。

另外一张图

SPI和API区别.png
上一篇 下一篇

猜你喜欢

热点阅读