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