samedi 21 mai 2016

MVP design pattern best practice

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 better? Why?

Aucun commentaire:

Enregistrer un commentaire