Explain the application of MVP mode in Android Development

1、 MVP introduction

With the increasing function of UI creation technology, the UI layer also performs more and more responsibilities. In order to better subdivide the functions of view and model, let view focus on data visualization and interaction with users, and let model only deal with relational data, MVP (Model View Presenter) mode based on MVC concept came into being.

In MVP mode, there are usually four elements:

(1) View: responsible for drawing UI elements and interacting with users (embodied as activity in Android);

(2) View interface: the interface that needs to be implemented by view. View interacts with presenter through view interface to reduce coupling and facilitate unit testing;

(3) Model: responsible for storing, retrieving and manipulating data (sometimes a model interface is implemented to reduce coupling);

(4) Presenter: as the intermediate link between view and model, it handles the responsible logic of user interaction.

2、 Why use MVP mode

In Android development, activity is not a controller in the standard MVC mode. Its primary responsibility is to load the application layout and initialize the user interface, accept and process the operation requests from users, and then respond. With the increasing complexity of the interface and its logic, the responsibilities of the activity class are increasing, so that it becomes huge and bloated. When we move the complex logic processing to another class (presenter), activity is actually the view in MVP mode. It is responsible for initializing UI elements, establishing the association between UI elements and presenter (listener, etc.), and also handling some simple logic (complex logic is handled by presenter)

In addition, recall how you unit tested the code logic when developing Android applications? Do you want to deploy the application to Android simulator or real machine every time, and then test it by simulating user operation? However, due to the characteristics of Android platform, each deployment takes a lot of time, which directly leads to the reduction of development efficiency. In MVP mode, presenters dealing with complex logic interact with view (activity) through interface. What does this mean? It shows that we can implement this interface through a custom class to simulate the behavior of activity and unit test the presenter, saving a lot of deployment and testing time.

3、 Similarities and differences between MVP and MVC

MVC pattern and MVP pattern have been used as a development pattern to separate UI layer and business layer for many years. When we choose a development mode, we first need to understand the advantages and disadvantages of this mode:

Both MVC and MVP patterns inevitably have one disadvantage: additional code complexity and learning cost.

This leads to the fact that these two development modes may not be very small applications.

However, compared with their advantages, this disadvantage can be basically ignored:

(1) Reduce coupling

(2) Clear division of module responsibilities

(3) Facilitate Test Driven Development

(4) Code reuse

(5) Hide data

(6) Code flexibility

There are also great differences between MVP and MVC. Some programmers choose not to use either mode, perhaps in part because they can't distinguish the differences between the two modes.

(reference article: http://www.infragistics.com/community/blogs/todd_snyder/archive/2007/10/17/mvc-or-mvp-pattern-whats-the-difference.aspx )

MVP mode:

MVC mode:

4、 An example of Android development using MVP

Having said so many theories, it's time to practice now.

Now let's implement such a demo on Android (as shown in the figure): you can read and access user information from EditText, or read and display user information from the background according to ID.

The page layout is very simple, so I won't introduce it. The following is coded according to the MVP principle:

Let's first look at the directory structure of java files:

It can be found that presenter interacts with model and view through interfaces, which not only reduces coupling, but also facilitates unit testing.

(1) First, we need a userbean to store user information

(2) Take another look at the view interface:

According to the requirements, view can read ID, firstname and LastName EditText and write firstname and LastName to define iuserview interface:

(3) Model interface:

Similarly, the model also needs to read and write these three fields and store them in a load (this is not our concern. There can be memory, file, database or remote server, but it has no impact on presenter and view). Define the iusermodel interface:

(4)Presenter:

At this point, the presenter can interact with the view and model through the interface:

(5)UserActivity:

Useractivity implements iuserview and view.onclicklistener interfaces, and has a userpresenter member variable:

Onclick method overridden:

It can be seen that view is only responsible for interacting with users, and leaves the data related logical operations to the presenter. After the presenter calls the model to process the data, it updates the information displayed by the view through iuserview.

The remaining methods of view and usermodel class are not the focus of our concern. If you are interested, you can click the link below to download.

The source code is here

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>