欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android-SPI學(xué)習(xí)筆記

 更新時(shí)間:2021年02月08日 09:31:06   作者:蒼耳  
這篇文章主要介紹了Android-SPI的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下

概述

SPI(Service Provider Interface, 服務(wù)提供方接口),服務(wù)通常是指一個(gè)接口或者一個(gè)抽象類(lèi),服務(wù)提供方是對(duì)這個(gè)接口或者抽象類(lèi)的具體實(shí)現(xiàn),由第三方來(lái)實(shí)現(xiàn)接口提供具體的服務(wù)。通過(guò)解耦服務(wù)與其具體實(shí)現(xiàn)類(lèi),使得程序的可擴(kuò)展性大大增強(qiáng),甚至可插拔?;诜?wù)的注冊(cè)與發(fā)現(xiàn)機(jī)制,服務(wù)提供者向系統(tǒng)注冊(cè)服務(wù),服務(wù)使用者通過(guò)查找發(fā)現(xiàn)服務(wù),可以達(dá)到服務(wù)的提供與使用的分離。

可以將 SPI 應(yīng)用到 Android 組件化中,很少直接使用 SPI,不過(guò)可基于它來(lái)擴(kuò)展其功能,簡(jiǎn)化使用步驟。

基本使用

1. 在低層 module_common 中聲明服務(wù)

public interface IPrinter {
  void print();
}

2. 在上層 module 中實(shí)現(xiàn)服務(wù)

// module_a -- implementation project(':module_common')
// com.hearing.modulea.APrinter
public class APrinter implements IPrinter {
  @Override
  public void print() {
    Log.d("LLL", "APrinter");
  }
}
// src/main/resources/META-INF/services/com.hearing.common.IPrinter
// 可以配置多個(gè)實(shí)現(xiàn)類(lèi)
com.hearing.modulea.APrinter

// ----------------------------------------------------------------//

// module_b -- implementation project(':module_common')
// com.hearing.moduleb.BPrinter
public class BPrinter implements IPrinter {
  @Override
  public void print() {
    Log.d("LLL", "BPrinter");
  }
}
// src/main/resources/META-INF/services/com.hearing.common.IPrinter
com.hearing.moduleb.BPrinter

3. 在其它上層 module 中使用服務(wù)

// implementation project(':module_common')
ServiceLoader<IPrinter> printers = ServiceLoader.load(IPrinter.class);
for (IPrinter printer : printers) {
  printer.print();
}

ServiceLoader.load

ServiceLoader 的原理解析從 load 方法開(kāi)始:

public static <S> ServiceLoader<S> load(Class<S> service) {
  // 獲取當(dāng)前線程的類(lèi)加載器
  ClassLoader cl = Thread.currentThread().getContextClassLoader();
  return ServiceLoader.load(service, cl);
}

public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) {
  // 創(chuàng)建 ServiceLoader 實(shí)例
  return new ServiceLoader<>(service, loader);
}

ServiceLoader實(shí)例創(chuàng)建

private LinkedHashMap<String,S> providers = new LinkedHashMap<>();

private ServiceLoader(Class<S> svc, ClassLoader cl) {
  service = Objects.requireNonNull(svc, "Service interface cannot be null");
  loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
  reload();
}

// Clear this loader's provider cache so that all providers will be reloaded.
public void reload() {
  providers.clear();
  // 創(chuàng)建了一個(gè)懶迭代器
  lookupIterator = new LazyIterator(service, loader);
}

LazyIterator

ServiceLoader 實(shí)現(xiàn)了 Iterable 接口,可以使用 iterator/forEach 方法來(lái)迭代元素,其 iterator 方法實(shí)現(xiàn)如下:

public Iterator<S> iterator() {
  return new Iterator<S>() {
    Iterator<Map.Entry<String,S>> knownProviders = providers.entrySet().iterator();

    public boolean hasNext() {
      if (knownProviders.hasNext()) return true;
      return lookupIterator.hasNext();
    }

    public S next() {
      // 如果 knownProviders 緩存中已經(jīng)存在,則直接返回,否則加載
      if (knownProviders.hasNext()) return knownProviders.next().getValue();
      return lookupIterator.next();
    }

    public void remove() {
      throw new UnsupportedOperationException();
    }
  };
}

上面使用了懶加載的方式,不至于一開(kāi)始便去加載所有服務(wù)實(shí)現(xiàn),否則反射影響性能。LazyIterator 類(lèi)如下:

private static final String PREFIX = "META-INF/services/";

private class LazyIterator implements Iterator<S> {
  Class<S> service;
  ClassLoader loader;
  Enumeration<URL> configs = null;
  Iterator<String> pending = null;
  String nextName = null;

  private LazyIterator(Class<S> service, ClassLoader loader) {
    this.service = service;
    this.loader = loader;
  }

  private boolean hasNextService() {
    if (nextName != null) {
      return true;
    }
    if (configs == null) {
      try {
        // 獲取服務(wù)配置文件
        String fullName = PREFIX + service.getName();
        if (loader == null)
          configs = ClassLoader.getSystemResources(fullName);
        else
          configs = loader.getResources(fullName);
      } catch (IOException x) {
        fail(service, "Error locating configuration files", x);
      }
    }
    while ((pending == null) || !pending.hasNext()) {
      if (!configs.hasMoreElements()) {
        return false;
      }
      // 解析服務(wù)配置
      pending = parse(service, configs.nextElement());
    }
    nextName = pending.next();
    return true;
  }

  private S nextService() {
    if (!hasNextService()) throw new NoSuchElementException();
    String cn = nextName;
    nextName = null;
    Class<?> c = null;
    try {
      // 反射通過(guò)類(lèi)加載器加載指定服務(wù)
      c = Class.forName(cn, false, loader);
    } catch (ClassNotFoundException x) {
      // throw Exception
    }
    if (!service.isAssignableFrom(c)) {
      // throw Exception
    }
    try {
      S p = service.cast(c.newInstance());
      providers.put(cn, p);
      return p;
    } catch (Throwable x) {
      // throw Exception
    }
    throw new Error();   // This cannot happen
  }

  public boolean hasNext() {
    return hasNextService();
  }

  public S next() {
    return nextService();
  }

  public void remove() {
    throw new UnsupportedOperationException();
  }
}

總結(jié)

ServiceLoader 的原理比較簡(jiǎn)單,其實(shí)就是使用一個(gè)懶迭代器,用時(shí)加載的方式可以減少性能損耗,在加載新服務(wù)的時(shí)候通過(guò)解析服務(wù)配置文件獲取配置的服務(wù),然后通過(guò)類(lèi)加載器去加載配置的服務(wù)實(shí)現(xiàn)類(lèi),最后將其實(shí)例返回。

SPI的優(yōu)點(diǎn)

  • 只提供服務(wù)接口,具體服務(wù)由其他組件實(shí)現(xiàn),接口和具體實(shí)現(xiàn)分離。

SPI的缺點(diǎn)

  • 配置過(guò)于繁瑣
  • 具體服務(wù)的實(shí)例化由ServiceLoader反射完成,生命周期不可控
  • 當(dāng)存在多個(gè)實(shí)現(xiàn)類(lèi)對(duì)象時(shí),ServiceLoader只提供了一個(gè)Iterator,無(wú)法精確拿到具體的實(shí)現(xiàn)類(lèi)對(duì)象
  • 需要讀取解析配置文件,性能損耗

以上就是Android-SPI學(xué)習(xí)筆記的詳細(xì)內(nèi)容,更多關(guān)于Android-SPI的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論