JAVA—API和SPI概念

目錄

概念

JDBC實例

自己實現一個SPI

總結



概念

英文:

What is the difference between Service Provider Interface (SPI) and Application Programming Interface (API)?

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.

Sometimes SPI and API overlap. For example in JDBC the Driver class is part of the SPI: If you simply want to use JDBC, you don't need to use it directly, but everyone who implements a JDBC driver must implement that class.

The Connection interface on the other hand is both SPI and API: You use it routinely when you use a JDBC driver and it needs to be implemented by the developer of the JDBC driver.

大致含義:

服務提供接口(SPI)和應用程序接口(API)有什麼不同?

具體來說,在JAVA類庫中,是什麼組成了API或者SPI?

API是你可以調用或者使用類/接口/方法等去完成某個目標的意思。

SPI是你需要繼承或實現某些類/接口/方法等去完成某個目標的意思。

換句話說,API制定的類/方法可以做什麼,而SPI告訴你你必須符合什麼規範。

有時候SPI和API互相重疊。例如JDBC驅動類是SPI的一部分:如果僅僅是想使用JDBC驅動,你不需要實現這個類,但如果你想要實現JdBC驅動那就必須實現這個類。

關於SPI和API:你使用JDBC驅動時可以使用現成的,該驅動由需要JDBC驅動開發者實現。

概括:

API:API由開發人員調用。

SPI:SPI是框架接口規範,需要框架開發人員實現。


JDBC實例

1.Class.forName("com.mysql.jdbc.Driver");
2.Connection conn = DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/test", "root", "123456");
3.Statement stmt = conn.createStatement();
4.ResultSet rs = stmt.executeQuery("select * from Users");

步驟2、3、4可以看做是API的調用。而DriverManager、Conn的編寫則可以看做是SPI,實現制定的接口。


自己實現一個SPI

現在我們來實現一個如JDBC加載驅動這樣模式的實例,我們定義一個消息驅動接口,實現並調用該驅動API。

消息驅動接口:

public interface IMsg {
    //發送消息,打印到控制檯
    void send(String msg);
}

消息驅動管理:

用來根據不同消息驅動的實現,獲得不同驅動。

public class MsgManage {

    private static final Map<String, Class<? extends IMsg>> map = new HashMap<String, Class<? extends IMsg>>();

    public static void register(String protocol, Class<? extends IMsg> cls) {
        map.put(protocol, cls);
    }

    public IMsg getMsgConnection(String protocol) {
        try {
            return map.get(protocol).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}

實現MyMsg消息驅動類:

public class MyMsg implements IMsg{

    static {
        MsgManage.register("my", MyMsg.class);
    }

    public void send(String msg) {
        System.out.println("[通過MyMsg實現加載]" + msg);
    }
}

實現YourMsg消息驅動類:

public class YourMsg implements IMsg{

    static {
        MsgManage.register("your", YourMsg.class);
    }

    public void send(String msg) {
        System.out.println("[通過YourMsg實現加載]" + msg);
    }
}

調用:

public class SpiD {

    @Test
    public void test() throws ClassNotFoundException {
        Class.forName("com.owl.zookeeper.use.spi.MyMsg");
        Class.forName("com.owl.zookeeper.use.spi.YourMsg");
        IMsg my = new MsgManage().getMsgConnection("my");
        IMsg your = new MsgManage().getMsgConnection("your");
        my.send("你好,世界");
        your.send("你好,世界");
    }
}

輸出:

[通過MyMsg實現加載]你好,世界
[通過YourMsg實現加載]你好,世界


總結

可以看出API主要是指調用已經實現的類去完成工作,使用者主要是程序開發人員。SPI主要是指框架擴展的規範,實現該規範可以擴展你框架的功能,使用者主要是框架開發人員。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章