Android组件化神器 —— ServicePool

组件化开发过程当中,随着组件愈来愈多,组件的以前的交互就会变得很是的复杂,此时组件间通讯变得尤为的重要,ServicePool就是为组件化而生,用最简单的方式进行组件间通讯。使用依赖注入,按需灵活注入组件。同时支持组件热插拔,达到组件即插即用的效果。可配置组件生命周期,作到组件按需建立和及时回收,充分利用懒加载的思想,有效解决组件初始化耗时致使的app启动速度问题。点击进入 项目地址java

ServicePool包含有 Activity路由, 组件路由等等最经常使用的组件化能力。除此以外,组件化开发过程当中有没有遇到过想使用某个已有的类,好比一个工具类的时候,发现这个工具类在当前类的上层,没法直接依赖和引用,而修改这个工具类的层级又会牵一发而动全身的问题? 有没有想要一个差别响应的能力,在不一样的组件中或者环境下,有着不一样的响应方式?有没有想要一个自适应场景的能力,自动适应当前环境(好比Java仍是Android环境,好比Debug环境仍是Release环境等等),从而使用最合适的功能。又有没有想过如何让组件作到像USB接口同样插上就能直接使用,拔掉也不影响主体功能的即插即用的效果。等等...。 下面就来具体介绍一下这个组件化神器——ServicePool!git

ServicePool基础能力

基础能力示意图

如上图所示:github

  1. 组件A,B是两个互不依赖的组件,A,B不能直接通讯
  2. 组件A,B分别经过AService, BService对外提供服务
  3. 组件A,B的接口协议存放在组件服务池pool, 分别是接口IA, IB
  4. 当组件B须要组件A的服务时,组件B使用IA接口向ServicePool申请, 由ServicePool建立并返回aService给组件B, 此时组件b可使用aService的服务了
  5. 同理, 组件A使用IB向ServicePool申请bService
  6. aService,bService由ServicePool建立和管理
  7. 全部Service对象的优先级生命周期能够经过@Service注解配置
/**
 * 服务池pool中
 *
 * IA.java
 */
public interface IA {
    void aName();
}


/**
 * 服务池pool
 *
 * IB.java
 */
public interface IB {
    void bName();
}
复制代码
/**
 * 组件A
 *
 * AService.java
 */
@Service
public class AService implements IA {

    @Override
    public String aName() {
        return "A Service";
    }
}
复制代码
/**
 * 组件B
 * 
 * BService.java
 */
@Service
public class BService implements IB {

    @Override
    public String bName() {
        return "B Service";
    }
}
复制代码
组件A中执行:
   IB b = ServicePool.getService(IB.class);
   System.out.println("I'm A Service, I can get " + b.bName());

输出: 
  I'm A Service, I can get B Service 组件B中执行: IA a = ServicePool.getService(IA.class); System.out.println("I'm B Service, I can get " + a.aName()); 输出: I'm B Service, I can get A Service 复制代码

支持经过path查找Service

/**
 * 服务池pool中
 * 
 * IPathService.java
 */
public interface IPathService {
    String pathServiceName();
}

复制代码
/**
 * 组件A中
 * 
 * PathService
 */
@Service(path = "pathService")
public class PathService implements IPathService {
    @Override
    public String pathServiceName() {
        return "Path Service";
    }
}
复制代码
组件B中执行:
    IPathService pathService = ServicePool.getService(IPathService.class);
    System.out.println(pathService.pathServiceName());

输出:
    Path Service
复制代码

典型应用场景:

  1. activity路由
  2. 混合开发中,能够经过path将桥接方法分发到对应执行器

指定Service优先级,按优先级顺序返回

组件按优先级顺序返回示意图

若是IA有多个实现,如上图所示,ServicePool会比较每一个实现优先级,来决定最终返回IA的哪一个实现缓存

  1. @Service注解标记一个实现类时候能够经过参数priority指定这个实现类的优先级
  2. 优先级值越大优先级越高, ServicePool默认会返回优先级最高的实现类对象
  3. 若是多个实现类优先级相同,那么返回会有不肯定性
  4. 也能够直接指定具体使用哪一个实现类,如ServicePool.getService(AService1.class)将会返回一个AService1对象
/**
 * 服务池pool中
 * 
 * IPriorityService.java
 */
public interface IPriorityService {
    int getPriority();
}
复制代码
/**
 * 组件A中
 * 
 * PriorityService1.java
 * PriorityService2.java
 */
@Service(priority = 1)
public class PriorityService1 implements IPriorityService {
    @Override
    public int getPriority() {
        return 1;
    }
}

@Service(priority = 1)
public class PriorityService2 implements IPriorityService {
    @Override
    public int getPriority() {
        return 2;
    }
}
复制代码
组件B中执行:
    IPriorityService priorityService = ServicePool.getService(IPriorityService.class);
    System.out.println("priority is " + priorityService.getPriority());
    
    priorityService = ServicePool.getService(PriorityService1.class);
    System.out.println("priority is " + priorityService.getPriority());
    
    priorityService = ServicePool.getService(PriorityService2.class);
    System.out.println("priority is " + priorityService.getPriority());
    
输出:
   priority is 2
   priority is 1
   priority is 2
复制代码

典型应用场景

  1. Java Library组件和Android Library组件使用不一样的服务, 如classloader等等。
  2. debug环境,release环境或者不一样的productFlavor使用不一样的服务, 如logger, Mock等等
  3. 不一样的业务场景使用不一样的服务

给服务对象指定生命周期

每一个由ServicePool建立的service对象都有各自生命周期,service对象的生命周期由ServicePool管理, 并由@Service注解配置生命周期类型。bash

  1. Service有once, temp, global三种生命周期类型.
  2. 指定Service的生命周期为once,@Service(scope=IService.Scope.once),每次ServicePool.getService()都会建立一个新的对象,对象使用后随gc自动被回收, scope默认为once
  3. 指定Service的生命周期为temp,@Service(scope=IService.Scope.temp),Service由WeakReference缓存,只适用无状态服务。
  4. 指定Service的生命周期为global,@Service(scope=IService.Scope.global),每次ServicePool.getService()拿到的都是同一个对象,App运行期间不会被回收
组件A中
/**
 * 
 * OnceService.java
 */
@Service(scope = IService.Scope.once)
public class OnceService implements LifecycleService {
}


/**
 * 
 * TempService.java
 */
@Service(scope = IService.Scope.temp)
public class TempService implements LifecycleService {
}


/**
 * 
 * GlobalService.java
 */
@Service(scope = IService.Scope.global)
public class GlobalService implements LifecycleService {
}
复制代码
组件B中执行:
    System.out.println(ServicePool.getService(OnceService.class) == ServicePool.getService(OnceService.class));
    //System.out.println(ServicePool.getService(TempService.class) == ServicePool.getService(TempService.class));//不可靠
    System.out.println(ServicePool.getService(GlobalService.class) == ServicePool.getService(GlobalService.class));

输出:
    false
    true
复制代码

组件初始化

未完待续....app

组件懒加载机制 & 禁用懒加载

未完待续....ide

Activity路由

未完待续....工具

依赖注入

未完待续....组件化

快速接入ServicePoolui

相关文章
相关标签/搜索