Introduction to MVP in Android Design Mode

Source: Internet
Author: User

Introduction to MVP in Android Design Mode
I. Overview

Model-View-Presenter (MVP) is an evolution of the MVC Model of the General Institute. They primarily aim to divide module responsibilities, reduce module coupling, facilitate testing, and improve code reuse, here, the MVP is analyzed for the Android platform.

 

1. hierarchical responsibility

Model: responsible for data retrieval, persistence, and other operations

View: draws the UI and interacts with users.

Presenter: as the coordination between Model and View, it is responsible for processing the business logic between the two.

 

2. Differences from MVC

One major difference between the MVP mode and the MVC mode is that the MVC mode allows direct communication between the View layer and the Model layer. from figure 1 and figure 2, we can see the difference between MVP and MVC.

In Figure 1MVC mode, the Model can directly update data to the View layer. Therefore, when a View has complex functions, the coupling between the View and the Model may be very high (and in android development, the Activity usually acts as the controller & view role, result Activity is very bloated ). the MVP mode does not have this problem. The View will abstract a series of interfaces that operate the UI (the Model layer can also be used), and the Presenter will obtain interfaces of the other two levels for processing the business logic. this not only reduces the coupling between the View and Model, but also makes it easier to perform unit tests.

 

Figure 1: MVC Mode

 

Figure 2: MVP Mode

 

 

3. Advantages and Disadvantages of MVP

Advantages: reduced coupling, more obvious responsibilities at the level, and easier Unit Testing

Disadvantage: the number of classes is exploding, and the Code complexity and learning cost are high. In some scenarios, reuse of presenter may produce interface redundancy.

 

 

2. Simple MVP instances use MVP to write a simple Login function. There are two buttons on the homepage: Login and Clear.

1. Project Structure

 

 

2. Model Layer

There is an entity UserBean used to carry data and UserBiz to determine the login data.

 

public class UserBean {    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

 

UserBiz abstracts an Interface

 

 

public interface UserBiz {    public void login(UserBean login);}

UserBizImpl instantiate the interface and call back the result to the Presenter through the login listener.

 

 

public class UsrBizImpl implements UserBiz{    private OnLoginListener listener;    public UsrBizImpl(OnLoginListener listener){        this.listener = listener;    }    @Override    public void login(UserBean login) {        boolean status = false;        String username,password;        username = login.getUsername();        password = login.getPassword();        if (username != null && "asdf".equals(username))            if (password != null && "123".equals(password))                status = true;        listener.loginStatus(status);    }}

3. View Layer

 

Abstract The View layer's control operations to some column interfaces.

 

public interface LoginView {    public String getUsername();    public String getPassword();    public void clearUsername();    public void clearPassword();    public void showMsg(String msg);}

Implement the control operation of this interface in the Activity and initialize the Presenter. This shows that there is no logic processing in the Activity, but it only performs data or behavior operations on the UI control, all actions are implemented using the Presenter interface, which greatly simplifies the Activity size in the project.

 

 

public class LoginActivity extends Activity implements LoginView{    private EditText username, password;    private Button login, clear;    private LoginPresenter loginPresenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        init();    }    private void init(){        loginPresenter = new LoginPresenterImpl(this);        username = (EditText) findViewById(R.id.username);        password = (EditText) findViewById(R.id.pass);        login = (Button) findViewById(R.id.login);        clear = (Button) findViewById(R.id.clear);        login.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                loginPresenter.login();            }        });        clear.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                loginPresenter.clear();            }        });    }    @Override    public String getUsername() {        return username.getText().toString();    }    @Override    public String getPassword() {        return password.getText().toString();    }    @Override    public void clearUsername() {        username.setText("");    }    @Override    public void clearPassword() {        password.setText("");    }    @Override    public void showMsg(String msg) {        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();    }}

4. Presenter Layer

 

In the Presenter layer, the Presenter masters all the interfaces of View and Model, and the Presenter can implement specific functions through the MV two-layer interface according to different business logic, separate M and V.

 

public class LoginPresenterImpl implements LoginPresenter, OnLoginListener{    private UserBiz userBiz;    private LoginView loginView;    public LoginPresenterImpl(LoginView loginView){        this.loginView = loginView;        userBiz = new UsrBizImpl(this);    }    @Override    public void login() {        UserBean login = new UserBean();        login.setUsername(loginView.getUsername());        login.setPassword(loginView.getPassword());        userBiz.login(login);    }    @Override    public void clear() {        loginView.clearPassword();        loginView.clearUsername();    }    @Override    public void loginStatus(boolean status) {        String msg;        if (status)            msg = "login succeed";        else            msg = "login failed";        loginView.showMsg(msg);    }}

5. Demo

 

Download CSDN

 

Iii. Summary

I had to use the MVP for a while and discussed it with my friends. Many people said it looked good, but it was very troublesome to use it. Indeed, most MVPs are currently used in Android projects. With some doubts, the author wants to know what style is used in the android native program. He has read some android source code before, however, they are all intermittent and have never written a blog for a long time. In the spirit of love for technology, we plan to re-enable the idea android source code (from the Android native system program to the framework). Now we hope we can stick to it, in addition, we will share and grow a blog series.

 

 

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.