I have code similar to this in all my observer classes that handle events fired by an event bus class. As you can see there are a lot of instanceof checks to choose the path of action needed to appropriately handle events, and I was wondering if this could be done more cleanly, eliminating the instanceof tests?
@Override
public void handleEvent(Event event) {
if (event instanceof DownloadStartedEvent) {
DownloadStartedEvent dsEvent = (DownloadStartedEvent)event;
dsEvent.getDownloadCandidateItem().setState(new BusyDownloadingState());
} else if (event instanceof DownloadCompletedEvent) {
DownloadCompletedEvent dcEvent = (DownloadCompletedEvent)event;
dcEvent.getDownloadCandidateItem().setState(new FinishedDownloadingState());
DownloadCandidate downloadCandidate = dcEvent.getDownloadCandidateItem(). getDownloadCandidate();
if (downloadCandidate.isComplete()) {
// start extracting
}
} else if (event instanceof DownloadFailedEvent) {
DownloadFailedEvent dfEvent = (DownloadFailedEvent)event;
dfEvent.getDownloadCandidateItem().setState(new FailedDownloadingState());
}
}