Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Watcher - EventBus Reinvented

Frank Xu
October 16, 2015

Watcher - EventBus Reinvented

一个更加简洁的观察者模式的实现,基于 RxJava 的 Subject,旨在解决现有 EventBus 难于在大规模代码的维护问题。

Frank Xu

October 16, 2015
Tweet

More Decks by Frank Xu

Other Decks in Programming

Transcript

  1. // Class is typically registered by the container. class EventBusChangeRecorder

    { @Subscribe public void recordCustomerChange(ChangeEvent e) { recordChange(e.getChange()); } } // somewhere during initialisation eventBus.register(new EventBusChangeRecorder()); // much later public void changeCustomer() { ChangeEvent event = getChangeEvent(); eventBus.post(event); } guava/EventBus
  2. // Class is typically registered by the container. class EventBusChangeRecorder

    { @Subscribe public void recordCustomerChange(ChangeEvent e) { recordChange(e.getChange()); } } // somewhere during initialisation eventBus.register(new EventBusChangeRecorder()); // much later public void changeCustomer() { ChangeEvent event = getChangeEvent(); eventBus.post(event); } guava/EventBus // Class is typically registered by the container. class EventBusChangeRecorder { @Subscribe public void recordCustomerChange(ChangeEvent e) { recordChange(e.getChange()); } } // somewhere during initialisation eventBus.register(new EventBusChangeRecorder()); // much later public void changeCustomer() { ChangeEvent event = getChangeEvent(); eventBus.post(event); }
  3. // Define events: public class MessageEvent { /* Additional fields

    if needed */ } // Prepare subscribers: eventBus.register(this); public void onEvent(AnyEventType event) { /* Do something */ }; // Post events: eventBus.post(event); greenrobot/EventBus
  4. // Define events: public class MessageEvent { /* Additional fields

    if needed */ } // Prepare subscribers: eventBus.register(this); public void onEvent(AnyEventType event) { /* Do something */ }; // Post events: eventBus.post(event); greenrobot/EventBus // Define events: public class MessageEvent { /* Additional fields if needed */ } // Prepare subscribers: eventBus.register(this); public void onEvent(AnyEventType event) { /* Do something */ }; // Post events: eventBus.post(event);
  5. Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); @Subscribe public void

    answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! } bus.register(this); @Produce public AnswerAvailableEvent produceAnswer() { // Assuming 'lastAnswer' exists. return new AnswerAvailableEvent(this.lastAnswer); } square/otto
  6. Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); @Subscribe public void

    answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! } bus.register(this); @Produce public AnswerAvailableEvent produceAnswer() { // Assuming 'lastAnswer' exists. return new AnswerAvailableEvent(this.lastAnswer); } square/otto Bus bus = new Bus(); bus.post(new AnswerAvailableEvent(42)); @Subscribe public void answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! } bus.register(this); @Produce public AnswerAvailableEvent produceAnswer() { // Assuming 'lastAnswer' exists. return new AnswerAvailableEvent(this.lastAnswer); }
  7. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } Watcher
  8. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } Watcher // Define an Event public interface PushWatcher extents Watchers.Watcher { void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } }
  9. guava EventBus greenrobot EventBus sqaure otto moai Watcher ᕚᑕਞق ✓

    ✓ ✓ ✓ ٖਂਞق ✓ ✓ ✗ ҁಋۖᥴᕬ҂ ✓ ᔄࣳਞق ✗ ✗ ✗ ✓ դᎱᰁҁဳ᯽҂ 322ᤈ ׁᩢ Guava ෫ ෫ RxJava/Guava Ԫկ݇හ੗ᤰ ᵱᥝ ᵱᥝ ᵱᥝ ӧᵱᥝ ັತݎ૲/ᦈᴅ ✗ ✗ ✗ ✓ Ԫկ٫ᑱ ✗ ✗ ✗ ✓ Ԫկᖀಥ ✓ ✗ ✓ ✓ ᯿຅ඪ೮ ✗ ✗ ✗ ✓ ग़ොᦈᴅ ✓ ✗ ✓ ✓ ᕚᑕᯈᗝ ොဩᕆ ොဩᕆ Bus & ොဩᕆ ᔄᕆ ളත෯ၾ௳ ✗ ✗ ✗ ✓ ࢧ᧣᷇ሲᴴګ ✗ ✗ ✗ ✓
  10. Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] { clazz }, new WatcherHandler(clazz) )

    ᧣አ঩ಓ Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] { clazz }, new WatcherHandler(clazz) )
  11. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 }
  12. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus
  13. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus Ԫկᔄڦ
  14. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus Ԫկᔄڦ Ԫկ֢ۖ
  15. WatcherHandler static class WatcherHandler implements InvocationHandler {
 private final Class<?

    extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } static class WatcherHandler implements InvocationHandler {
 private final Class<? extends Watcher> clazz;
 WatcherHandler(Class<? extends Watcher> clazz) {
 this.clazz = clazz;
 }
 
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 if (method.getDeclaringClass() == Object.class) {
 return method.invoke(this, args);
 }
 instance.trigger(clazz, method, args);
 return null;
 }
 } EventBus Ԫկᔄڦ Ԫկ֢ۖ ֢ۖӤӥ෈
  16. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } ᔄࣳਞق & ᯿຅
  17. // Define an Event public interface PushWatcher extents Watchers.Watcher {

    void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } } ᔄࣳਞق & ᯿຅ // Define an Event public interface PushWatcher extents Watchers.Watcher { void notify(Message message); } // Publishing an Event Watchers.of(PushWatcher.class).notify(message); // Subscribing to an Event public class MyFragment extends Fragment implement PushWatcher { public void notify(Message message) { } public void onStart() { Watchers.bind(this); } }
  18. ᷇ሲᴴګ @Watchers.Config(
 sample = 50,
 backpressureDrop = true,
 subject =

    Watchers.Subjects.BEHAVIOR
 )
 public interface LoadingWatcher extends Watchers.Watcher {
 void chapterProgress(LoadingProgress progress);
 }
  19. ᷇ሲᴴګ @Watchers.Config(
 sample = 50,
 backpressureDrop = true,
 subject =

    Watchers.Subjects.BEHAVIOR
 )
 public interface LoadingWatcher extends Watchers.Watcher {
 void chapterProgress(LoadingProgress progress);
 } @Watchers.Config(
 sample = 50,
 backpressureDrop = true,
 subject = Watchers.Subjects.BEHAVIOR
 )
 public interface LoadingWatcher extends Watchers.Watcher {
 void chapterProgress(LoadingProgress progress);
 }