0

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:

  1. 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);
    
  2. 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.

rafalry
  • 2,620
  • 6
  • 24
  • 39

1 Answers1

1

I think that it all boils to what you are trying to achive in terms of your code :) not functionality because both ways work :). For me second approch is clean an lean it states what is needed. It may not be loose coupled code but clean and obvious and I think that this is the added value of second approach :).

damiankolasa
  • 1,508
  • 9
  • 8
  • Thank you, fatfredyy. I was rather looking for an answers that would justify one or the other approach more formally. – rafalry Nov 26 '12 at 07:57
  • It might be difficult since software writing is creative and there are as many ways to write something as there are programmers. Only tought justification is that in your IDE it will show you clear referencess between the code but with observers and event listeners it won't :) – damiankolasa Nov 26 '12 at 08:19