Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP design pattern best practice [closed]

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's an alternative implementation of 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 more correct implementation of MVP pattern? Why?

like image 569
CarlLee Avatar asked Oct 31 '22 03:10

CarlLee


1 Answers

My short answer:

I would say the first one.

My long answer:

Basically MVP has two variants: Passive View and Supervising Presenter

Your pseudo classes creates an implementation of Passive View.

To see the difference: Please check the first answer here. It describes them and the difference between them perfectly, therefore I think it is not needed to copy here the content.

Reason of my answer:

Main idea of Passive View to have the view as dumb as possible. It simply notifies its presenter when some user action has been taken place and exposes accessors and mutators to get and set values from/on the GUI. All of these is done to achieve maximal testability on view-level.

Based on this, the view should not know that it should provide the value from your input textbox when the button gets pressed. It just should notify the presenter that the button is pressed and expose getters for the presenter to collect any user input that it wants.

like image 63
DVarga Avatar answered Nov 10 '22 00:11

DVarga