| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
ghfn8755
8年前发布

RxJava替代EventBus:RxBus

   <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>    
 本文由用户 ghfn8755 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1480492679384.html
EventBus RxJava