Best Practices for MVP Template Design

Consider the following pseudo code that implements the MVP pattern:

interface Presenter { void onSendClicked(); } interface View { String getInput(); void showProgress(); void hideProgress(); } class PresenterImpl implements Presenter { // ...ignore other implementations void onSendClicked() { String input = view.getInput(); view.showProgress(); repository.store(input); view.hideProgress(); } } class ViewImpl implements View { // ...ignore other implementations void onButtonClicked() { presenter.onSendClicked(); } String getInput() { return textBox.getInput(); } void showProgress() { progressBar.show(); } void hideProgress() { progressBar.hide(); } } 

And here is an alternative implementation of the MVP pattern:

 interface Presenter { void saveInput(String input); } interface View { void showProgress(); void hideProgress(); } class PresenterImpl implements Presenter { // ...ignore other implementations void saveInput(String input) { view.showProgress(); repository.store(input); view.hideProgress(); } } class ViewImpl implements View { // ...ignore other implementations void onButtonClicked() { String input = textBox.getInput(); presenter.saveInput(intput); } void showProgress() { progressBar.show(); } void hideProgress() { progressBar.hide(); } } 

Which one is the more correct implementation of the MVP pattern? Why?

+5
source share
1 answer

My short answer is:

I would say the first one.

My long answer is:

Basically, MVP has two options: Passive Viewing and Controlling Presenter

Your pseudo-classes create an implementation of a passive view.

To see the difference: Please check the first answer here . He describes them and distinguishes them perfectly, so I think there is no need to copy the content here.

Reason for my answer:

The basic idea of ​​a passive performance is to look as deep as possible. He simply notifies his presenter that some user action has been performed and provides access to accessories and mutators for receiving and setting values ​​from / in the graphical interface. All this is done to achieve maximum testability at the presentation level.

Based on this, the view should not know that it should provide a value from your text input field when the button is clicked. He just needs to notify the presenter that the button is pressed and set getters for the presenter to collect any user input that he wants.

+2
source

All Articles