Java服務提供者架構_Effective Java 2.0_Item 1知識點__java

來源:互聯網
上載者:User

文章作者: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 interfacepublic interface Service {    ... // Service-specific methods go here}

Provider Registration API 和 Service Access API:

// Noninstantiable class for service registration and accesspublic 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 interfacepublic interface Provider {    Service newService();}
3. 服務提供者架構圖

參考資料:

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

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

3、Effective Java 2.0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.