RxJava替代EventBus:RxBus
ghfn8755
8年前
<p>由于最近工程中依赖于RxAndroid与RxJava,因此以RxJava模式实现EventBus相同的API,移除重复功能的依赖,达到精简工程的目的。</p> <h3><strong>第一步:构造单例</strong></h3> <ol> <li>在EventBus中 <pre> <code class="language-java">EventBus.getDefault();</code></pre> </li> <li>构造RxBus相同的API <pre> <code class="language-java">public class RxBus { private static volatile RxBus instance; private RxBus() {} public static RxBus getDefault() { if (instance == null) { synchronized (RxBus.class) { if (instance == null) { instance = new RxBus(); } } } return instance; }</code></pre> </li> <li>在RxBus中 <pre> <code class="language-java">RxBus.getDefault();</code></pre> </li> </ol> <h3><strong>第二步:事件注解</strong></h3> <ol> <li>在EventBus中 <pre> <code class="language-java">@Subscribe public void onMessageEvent(MessageEvent event) {/* Do something */};</code></pre> </li> <li> <p>构造RxBus相同的API</p> <pre> <code class="language-java">public class RxBus { //省略第一步... @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Subscribe {} }</code></pre> </li> <li>在RxBus中 <pre> <code class="language-java">@RxBus.Subscribe public void onMessageEvent(MessageEvent event) {/* Do something */};</code></pre> </li> </ol> <h3><strong>第三步:订阅事件</strong></h3> <ol> <li>在EventBus中 <pre> <code class="language-java">@Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); }</code></pre> </li> <li> <p>构造RxBus相同的API</p> <pre> <code class="language-java">public class RxBus { //省略第一步... private final Subject<Object, Object> bus = new SerializedSubject<>(PublishSubject.create()); private final ArrayMap<String, List<Subscription>> subscriptionArray = new ArrayMap<>(); public void register(final Object observable) { boolean isRegisterSuccess = false; final String subscriptionKey = observable.getClass().getName(); //反射获取方法列表 Method[] methods = observable.getClass().getMethods(); for (Method method : methods) { //如果方法不存在Subscribe注解则不做处理 if (!method.isAnnotationPresent(Subscribe.class)) continue; final Method subscriptionMethod = method; //获取方法参数类型,即:MessageEvent.class Class<?> key = method.getParameterTypes()[0]; //订阅MessageEvent.class类型实例,ofType作用已filter相近 Subscription subscription = bus.ofType(key).subscribe(new Action1<Object>() { @Override public void call(Object value) { try { //反射赋值,即传递事件值 subscriptionMethod.setAccessible(true); subscriptionMethod.invoke(observable, value); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(subscriptionKey + " isn't allowed to register!"); } } }); //存储订阅实例列表,用于后续解除注册 List<Subscription> subscriptions; if (subscriptionArray.containsKey(subscriptionKey)) { subscriptions = subscriptionArray.get(subscriptionKey); } else { subscriptions = new ArrayList<>(); } subscriptions.add(subscription); subscriptionArray.put(subscriptionKey, subscriptions); isRegisterSuccess = true; } //如果注册的类中不存在Subscribe注解方法,则抛出异常提醒 if (!isRegisterSuccess) throw new RuntimeException(subscriptionKey + " has no any RxBuxSubscribe Event!"); } //省略第二步... }</code></pre> </li> <li>在RxBus中 <pre> <code class="language-java">@Override public void onStart() { super.onStart(); RxBus.getDefault().register(this); }</code></pre> </li> </ol> <p>取消订阅</p> <ol> <li>在EventBus中 <pre> <code class="language-java">@Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); }</code></pre> </li> <li> <p>构造RxBus相同的API</p> <pre> <code class="language-java">public class RxBus { //省略第一步... //省略第三步... public void unregister(Object observable) { String subscriptionKey = observable.getClass().getName(); if (!subscriptionArray.containsKey(subscriptionKey)) return; List<Subscription> subscriptions = subscriptionArray.get(subscriptionKey); for (Subscription subscription : subscriptions) { //如果已订阅,则取消订阅 if (!subscription.isUnsubscribed()) subscription.unsubscribe(); } subscriptionArray.remove(subscriptionKey); } //省略第二步... }</code></pre> </li> <li>在RxBus中 <pre> <code class="language-java">@Override public void onStop() { super.onStop(); RxBus.getDefault().unregister(this); }</code></pre> </li> </ol> <p>发送事件</p> <ol> <li>在EventBus中 <pre> <code class="language-java">EventBus.getDefault().post(new MessageEvent());</code></pre> </li> <li> <p>构造RxBus相同的API</p> <pre> <code class="language-java">public class RxBus { //省略第一步... //省略第三步... //省略第四步... public void post (Object event) { bus.onNext(event); } //省略第二步... }</code></pre> </li> <li>在RxBus中 <pre> <code class="language-java">RxBus.getDefault().post(new MessageEvent());</code></pre> </li> </ol> <p>关于ThreadMode</p> <p>在订阅时根据需要使用RxJava的Schedulers即可。</p> <p> </p> <p>来自:http://www.jianshu.com/p/4eb68b206fe0</p> <p> </p>