MVP design pattern best practice

CarlLee

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?

DVarga

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MVP in GWT : Best design practice

From Dev

Google Analytics with the MVP design pattern

From Dev

Best practice for interface design

From Dev

Is there any best practice for packaging MVP layers?

From Dev

Design pattern best practices

From Dev

In a MVP design pattern, who is responsible for accessing the database

From Dev

What is the correct standard to implement MVP Design pattern

From Dev

Best Design Pattern Practice Nested Bluetooth LE Device inside an Extended Bluetooth Device Class

From Dev

What is the best practice or design pattern to maintain sync activity across multiple views?

From Dev

best practice data design in firebase

From Dev

simple responsive design \ best practice

From Dev

Best design pattern for event listeners

From Dev

best pattern design for my project

From Dev

Design pattern to consume WebAPI from MVP Winform Client

From Dev

Where to place logic in MVP (Passive Controller) design pattern

From Dev

Correct use of Model in Presenter with MVP-VM Design Pattern

From Dev

What are the alternatives of Module View Presenter(MVP) design pattern?

From Dev

In MVP design pattern, can a view have more than one presenter?

From Dev

Data from Database Using MVP-VM Design Pattern

From Dev

OCaml order of pattern matches best practice

From Dev

Good practice design pattern for Exception handling

From Dev

Android AsyncTask best practice / design paterns?

From Dev

What is the best practice database design for transactions aggregation?

From Dev

Android AsyncTask best practice / design paterns?

From Dev

Best design pattern for switching between hardware interfaces

From Dev

Best Design Pattern for a WPF MVVM application

From Dev

What is best Ruby Class design / pattern for this scenario?

From Dev

Best OOP design pattern for static class DbTable

From Dev

Best Design Pattern to execute steps in python

Related Related

  1. 1

    MVP in GWT : Best design practice

  2. 2

    Google Analytics with the MVP design pattern

  3. 3

    Best practice for interface design

  4. 4

    Is there any best practice for packaging MVP layers?

  5. 5

    Design pattern best practices

  6. 6

    In a MVP design pattern, who is responsible for accessing the database

  7. 7

    What is the correct standard to implement MVP Design pattern

  8. 8

    Best Design Pattern Practice Nested Bluetooth LE Device inside an Extended Bluetooth Device Class

  9. 9

    What is the best practice or design pattern to maintain sync activity across multiple views?

  10. 10

    best practice data design in firebase

  11. 11

    simple responsive design \ best practice

  12. 12

    Best design pattern for event listeners

  13. 13

    best pattern design for my project

  14. 14

    Design pattern to consume WebAPI from MVP Winform Client

  15. 15

    Where to place logic in MVP (Passive Controller) design pattern

  16. 16

    Correct use of Model in Presenter with MVP-VM Design Pattern

  17. 17

    What are the alternatives of Module View Presenter(MVP) design pattern?

  18. 18

    In MVP design pattern, can a view have more than one presenter?

  19. 19

    Data from Database Using MVP-VM Design Pattern

  20. 20

    OCaml order of pattern matches best practice

  21. 21

    Good practice design pattern for Exception handling

  22. 22

    Android AsyncTask best practice / design paterns?

  23. 23

    What is the best practice database design for transactions aggregation?

  24. 24

    Android AsyncTask best practice / design paterns?

  25. 25

    Best design pattern for switching between hardware interfaces

  26. 26

    Best Design Pattern for a WPF MVVM application

  27. 27

    What is best Ruby Class design / pattern for this scenario?

  28. 28

    Best OOP design pattern for static class DbTable

  29. 29

    Best Design Pattern to execute steps in python

HotTag

Archive