I'm evaluating if CDI Events could make sense for my new application. So far I always worked with MVP architectures where the View only has the UI elements and exposes them in public getters, whereas the Presenter registers click listeners on them.
I came around CDI Events and thought about to fire the click events in the View classes directly, and simply only observe these events in my Presenters.
Could you tell me which approach is the better one? Or why would you in general chose one over the other approach?
MVP:
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
}
});
}
}
CDI Events:
class LoginView {
private Button loginButton;
@Inject
private Events<LoginEvent> 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();
}
}
(this example uses Vaadin, but framework choice should not matter for my question in general)
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.