(4.6.22.5)来吧,是时候撸一份自己的RxJava框架啦:强撸为eventbus

来源:互联网 发布:农村淘宝开业计划书 编辑:程序博客网 时间:2024/05/20 11:35

RxJava和EventBus的区别,无非就是EventBus的全局可达性

实现思路

  • 消费页面

    • 页面入口处,从全局变量中获取到 对应的 业务控制器,并 加入消费者
  • 生产页面

    • 调用处,构建一个业务控制器,构建自己的生产者,并将业务控制器存入全局变量

实践

private static HashMap<String,IPublisher> publishSaved = new HashMap<String,IPublisher>();     /****************************** A. 扩展成EventBus *************************************************/    @Override    public IPublisher<T> name(String t) {        name = t;        return this;    }    @Override    public synchronized IPublisher<T> save() {        if (name  == null)            throw new RuntimeException("Publisher未命名");        if (publishSaved.containsKey(name))            throw new RuntimeException("Publisher重名");        publishSaved.put(name,this);        return this;    }    @Override    public void destroy() {        publishSaved.remove(name);    }    public static <T> IPublisher<T> getPublisher(String name){        return (IPublisher<T>)publishSaved.get(name);    }

Test

消费页面—监听页面

    @Override    protected void onCreate(Bundle savedInstanceState) {    ...        Publisher<T>.getPublisher("消费页面A")            .bind(消费者);    ...}    @Override    protected void onDestroy() {      Publisher<T>.getPublisher("消费页面A")       .destroy();        super.onDestroy();    }

生产页面

    protected void xxx(Bundle savedInstanceState) {    ...        Publisher<T>.create(xxx)            .post();    ...}
0 0
原创粉丝点击