Let's say we have a Java application designed with MVP with a page displaying some independent modules. There are, among others, two independent MVP modules in the app we will take into consideration. There there is a functionality in the app that requires the mentioned modules to communicate. The modules offer interfaces (APIs) A and B devoted for the functionality. There is also Manager (M) to handle the logic for this functionality. The manager could traverse (or use some context) through the page to obtain the references to the modules that should take part in the considered functionality.
The below snippets of Manager's code present the approaches I'm thinking of:
Using asynchronous events through the EventBus
eventBus.addHandler(Event.CHECK_A_IS_OK, new CheckAIsOkHandler(Event e){ eventBus.fireEvent(Event.DO_SOMETHING_ON_B, e.getValue(); }); eventBus.fireEvent(Event.CHECK_A_IS_OK);
Using direct references
A a; B b; void findModules(){ a = context.findA(); b = context.findB(); } ... if (a.check()){ b.doSomething(B.getValue()); }
The questions are:
Is it ok to use EventBus for the regular communication between objects (checking conditions, returning values etc)? Or it is better to use events pattern only for real events (I mean informing subscribers that something have happend)? Is it against loose coupling to follow the second approach? Maybe some other pattern would be more suitable?
Intuitively, the second apprach is cleaner for me.