title: MVP - register click listener or use CDI events?
body: <p>I'm evaluating if <strong>CDI Events</strong> could make sense for my new application. So far I always worked with <code>MVP</code> architectures where the <code>View</code> only has the UI elements and exposes them in public getters, whereas the <code>Presenter</code> registers click listeners on them.</p>
  
  <p>I came around <code>CDI Events</code> and thought about to fire the click events in the <code>View</code> classes directly, and simply only <code>observe</code> these events in my <code>Presenters</code>.</p>
  
  <p>Could you tell me which approach is the better one? Or why would you in general chose one over the other approach?</p>
  
  <p><em>MVP</em>:</p>
  
  <pre><code>class LoginView {   
      private Button loginButton;
  
      public void getButton() {
          return loginButton;
      }
  }
  
  
  class LoginPresenter {
      @Inject
      private LoginView view;
  
      public LoginPresenter() {
          view.getButton.addClickListener(new ClickListener() {
              @Override
              public void buttonClick(ClickEvent event) {
                  //perform the login business logic
              }
          });
      }
  }
  </code></pre>
  
  <p><em>CDI Events</em>:</p>
  
  <pre><code>class LoginView {   
      private Button loginButton;
  
      @Inject
      private Events&lt;LoginEvent&gt; events;
  
      public LoginView() {
          loginButton.addClickListener(new ClickListener() {
              @Override
              public void buttonClick(ClickEvent event) {
                  events.fire(new LoginEvent("login"));
              }
          });
      }
  }
  
  
  class LoginPresenter {
      private void listenLogin(@Observes LoginEvent evt) {
          //perform the login business logic
      }
  }
  
  class LoginEvent extends EventObject {
      public LoginEvent(String source) {
          super();
      }
  }
  </code></pre>
  
  <p>(this example uses Vaadin, but framework choice should not matter for my question in general)</p>
  
  <p>To me the main difference is: CDI requires no getters for the UI and no view member variables in the Presenter. On the downside, I need to create an extra event class for every event that is to be fired.</p>