Detailed explanation of MVP architecture decomposition and implementation in Android

1. Overview

The traditional Android development architecture is generally MVC mode,

It looks very good from the logic alone, which is similar to the development mode when we do web development, but in the actual development, the view corresponds to the layout file. In fact, the data binding operation and event processing code in the layout file are all in the activity. The activity is like both view and controller (mvvp architecture includes data binding), resulting in too heavy responsibilities in the activity, High coupling. It is very troublesome to modify and maintain.

2. MVP introduction

In MVP architecture, view corresponds to activity and is responsible for drawing view and interacting with users

The model is still the business logic and entity model, and the presenter is responsible for completing the interaction between the view and the model.

(1) Model layer

The work done in the model layer is the implementation of specific business logic processing, which is accompanied by the processing of various data in the program. If it is more complex, it is necessary to implement an interface to loosely couple.

(2) View layer

The view layer is very light and thin. It is responsible for displaying data, providing a friendly interface and interacting with users. Activity and fragment under MVP are reflected in this layer. Activity generally does some work such as loading UI view, setting listening, and then handing it over to the presenter. Therefore, it is necessary to hold the reference of the corresponding presenter. Process some basic UI logic to determine whether it is empty.

(3) Presenter layer

The presenter layer handles the distribution of various program logic. After receiving feedback commands, timing commands, system commands and other instructions on the UI of the view layer, the distribution processing logic is handed over to the model layer for specific business operations.

Differences between MVP architecture and MVC Architecture:

In MVC, model and view are allowed to interact, while in MVP, the interaction between model and view is completed by presenter. Another point is that the interaction between presenter and view is through the interface.

3. MVP implementation

A hundred words is better than one action. Implement a simple login operation.

The project structure is as follows:

(1) Model layer implementation

First implement the user entity class:

The model layer mainly implements the business logic processing. In this case, the main logic processing is login. An interface and an implementation class are extracted to simulate the login operation in the login operation. Thread. Sleep() simulates the time-consuming operation. Because it is a time-consuming operation, the login status is notified through a callback interface.

Model layer interface:

Model layer interface implementation:

Callback interface:

(2) View layer implementation

For the view layer interface definition, first consider the functional operations, and then consider:

View layer interface:

The implementation of the view layer is actually an activity. You can see that the amount of code of the activity is greatly reduced and the logic is clear:

(3) Presenter layer implementation

The presenter layer is the bridge between the model layer and the view layer. In this paper, an interface and an implementation class are still abstracted. The definition mainly depends on the operation of the function, such as login:

Presenter interface:

Presenter layer implementation:

The presenter layer is the bridge between the model layer and the view layer. The model layer and the view layer do not communicate directly. Therefore, the presenter layer needs the implementation classes of the model layer and the view layer. It obtains important parameters from the view layer and gives them to the model layer to call the business logic for processing. The results and feedback after execution are presented to the view layer.

Download the source code of this article

This paper simply uses MVP architecture to realize login operation. At present, the mainstream development combines rxjava, retrofit and MVP to make the logic clearer. The next article will introduce this part in detail.

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
分享
二维码
< <上一篇
下一篇>>