Comparison between MVP mode and MVC mode in Android (including examples)

Source: Internet
Author: User

Comparison between MVP mode and MVC mode in Android (including examples)
MVP Introduction

Model-View-Presenter is a derivative of the MVC pattern. The main purpose is to decouple and make the project easy to maintain.

Model is still the business logic and entity Model View is often implemented by Activity, including Presenter reference. All you need to do is call the corresponding method in Presenter when there is an interaction. Presenter is responsible for completing the interaction between views and models, retrieving data from the Model, and returning the processed data to the View. Why use MVP

In the past Android development, Activity is not a Controller in the standard MVC mode. It loads the application layout and initializes the user interface, and accepts and processes operation requests from users, then make a response. However, as the complexity of the interface and its logic increases, the responsibilities of the Activity class keep increasing, resulting in a huge bloated. When we move the complex logic processing to another class (Presneter), the Activity is actually the View in MVP mode, which is responsible for UI element initialization, establish the association between the UI element and the Presenter (such as Listener), and process some simple logic by yourself (complicated logic is handled by the Presenter ).
For testing, in MVP mode, the Presenter that processes complex logic interacts with the View (Activity) through interfaces. We can use a custom class to implement this interface to simulate the Activity behavior to perform unit tests on the Presenter, saving a lot of time for deployment and testing.

MVC Introduction Model is part of the application's data logic. View is the part of the application that processes data display. Controller is the part of an application that processes user interaction.
For details, click here to compare

MVP mode:

The View does not directly interact with the Model, but indirectly interacts with the Model through the Presenter interface, it is more conducive to adding unit tests. Generally, the View and Presenter are one-to-one. However, complicated views may be bound to multiple presenters to process logic.

MVC mode:

The View can directly interact with the Model. The Controller is behavior-based and can be shared by multiple views. You can decide which View example to display.

An example of login registration.

Directory structure

Model

The Model is the User information, which is omitted in the project. Of course, you can create a User class.

public class UserBean {   private String mFirstName ;   private String mLastName ;   public UserBean (String firstName, String lastName) {     this .mFirstName = firstName;     this .mLastName = lastName;   }   public String getFirstName() {     return mFirstName ;   }   public String getLastName() {     return mLastName ;   }}
View
public class LoginActivity extends Activity implements LoginView, View.OnClickListener {  private ProgressBar progressBar;    private EditText username;    private EditText password;    private LoginPresenter presenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        progressBar = (ProgressBar) findViewById(R.id.progress);        username = (EditText) findViewById(R.id.username);        password = (EditText) findViewById(R.id.password);        findViewById(R.id.button).setOnClickListener(this);        presenter = new LoginPresenterImpl(this);    }    @Override public void onClick(View v) {        presenter.validateCredentials(username.getText().toString(), password.getText().toString());    }    ...    ...

You can see that LoginActivity implements has two interfaces: LoginView and View. OnClickListener. LoginView is used in the Presenter to communicate with the Activity.The onClick () method calls the presenter for transaction processing.

LoginView. java

public interface LoginView {    void showProgress();    void hideProgress();    void setUsernameError();    void setPasswordError();    void navigateToHome();}
Presenter
public class LoginPresenterImpl implements LoginPresenter, OnLoginFinishedListener {    private LoginView loginView;    private LoginInteractor loginInteractor;    public LoginPresenterImpl(LoginView loginView) {        this.loginView = loginView;        this.loginInteractor = new LoginInteractorImpl();    }    @Override public void validateCredentials(String username, String password) {        if (loginView != null) {            loginView.showProgress();        }        loginInteractor.login(username, password, this);    }    @Override public void onUsernameError() {        if (loginView != null) {            loginView.setUsernameError();            loginView.hideProgress();        }    }    ...    ...

It can be found that in the onUsernameError () method, the processed results are returned to the Activity through the LoginView interface for display.
So far, the entire process has passed,M stores data, V interaction, and P processing logic. V and P communicate through interfaces.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.