0

So i have created a event bus using RxJava and it looks like this:

    public class RxBus {

  private final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create());

  public void send(Object o) {
    _bus.onNext(o);
  }

  public Observable<Object> toObserverable() {
    return _bus;
  }
}

that is basically it and i made it a singleton(or inject it using DI) and the subscription then would look something like this:

_rxBus.toObserverable()
.subscribe(new Action1<Object>() {
  @Override
  public void call(Object event) {

    if(event instanceof TapEvent) {
      _doSomething();

    }else if(event instanceof SomeOtherEvent) {
      _doSomethingElse();
    }
  }
});

So in doing the subscription i am noticing i am using alot of instanceof calls. which makes me think the visitor pattern should be used as there are many types with the same base type (event). What would be a good way to use visitor pattern here ? I made the bus following this example

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Hi, I want to ask you, why you would need a eventbus, if you are using RxJava? Each observable is an eventbus itself. You could inject a service with methods which return observables and just subscribe to them. You do not need the overhead of many subscriptions for only one stream, which are using reflection in order to check which item is flowing downstreams. Please have a look at this article: https://medium.com/mobiwise-blog/use-rxjava-instead-of-event-bus-libraries-aa78b5023097#.bd9kp7e8m and http://stackoverflow.com/questions/35685577/what-is-the-difference-between-eventbus-and-rxjava – Sergej Isbrecht Nov 01 '16 at 19:56
  • the problem is my entire project is not reactive. we are not using observables anywhere. we had an otto bus and then swapped it with rxJava bus as per my code. we wanted to quietly swap otto for rxJava bus without much learning curve. – j2emanue Nov 01 '16 at 20:13
  • You can easily wrap everthing in Observable-adapters one by one. Just hava a look how netflix did it step by step. But using RxJava for event-bus only is a huge no no, because you do not gain any benefits from it instead of using a tested framework like otto. RxJava gives you the benefit of composing, which you are not using. – Sergej Isbrecht Nov 01 '16 at 20:32

0 Answers0