Share
Embed
Watcher - EventBus Reinvented
一个更加简洁的观察者模式的实现,基于 RxJava 的 Subject,旨在解决现有 EventBus 难于在大规模代码的维护问题。
Frank Xu
October 16, 2015
Transcript
52 slides
-
EventBus ฎՋԍ • ݎ-ᦈᴅཛྷࣳҁPublish - Subscribe҂ • guava/EventBus • greenrobot/EventBus
• square/otto • moai/Watcher -
// 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); } 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); } -
// 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); 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); -
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); } 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); } -
// 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); } } 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); } } -
guava EventBus greenrobot EventBus sqaure otto moai Watcher ᕚᑕਞق ✓
✓ ✓ ✓ ٖਂਞق ✓ ✓ ✗ ҁಋۖᥴᕬ҂ ✓ ᔄࣳਞق ✗ ✗ ✗ ✓ դᎱᰁҁဳ᯽҂ 322ᤈ ׁᩢ Guava ෫ ෫ RxJava/Guava Ԫկ݇හᤰ ᵱᥝ ᵱᥝ ᵱᥝ ӧᵱᥝ ັತݎ/ᦈᴅ ✗ ✗ ✗ ✓ Ԫկ٫ᑱ ✗ ✗ ✗ ✓ Ԫկᖀಥ ✓ ✗ ✓ ✓ ᯿ඪ೮ ✗ ✗ ✗ ✓ ग़ොᦈᴅ ✓ ✗ ✓ ✓ ᕚᑕᯈᗝ ොဩᕆ ොဩᕆ Bus & ොဩᕆ ᔄᕆ ളත෯ၾ௳ ✗ ✗ ✗ ✓ ࢧ᧣᷇ሲᴴګ ✗ ✗ ✗ ✓ -
Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] { clazz }, new WatcherHandler(clazz) )
᧣አಓ Proxy.newProxyInstance( clazz.getClassLoader(), new Class[] { clazz }, new WatcherHandler(clazz) ) -
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; } } -
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 -
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 Ԫկᔄڦ -
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 Ԫկᔄڦ Ԫկ֢ۖ -
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 Ԫկᔄڦ Ԫկ֢ۖ ֢ۖӤӥ -
// 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); } } ᔄࣳਞق & ᯿ // 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); } } -
ളත෯ၾ௳ 2 1 ᦈᴅ ݎ ἕᦊᤈԅ 2 ๋ᬪӞེ 2 զڹಅํ
2 1 3 3 3 3 -
᷇ሲᴴګ @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); } @Watchers.Config( sample = 50, backpressureDrop = true, subject = Watchers.Subjects.BEHAVIOR ) public interface LoadingWatcher extends Watchers.Watcher { void chapterProgress(LoadingProgress progress); }