directory

MVP stuff (1)… Speak with the scene

MVP stuff (2)… A Preliminary study on MVC Architecture

MVP stuff (3)… Using MVC in Android (Part 1)

MVP stuff (4)… Using MVC in Android (Part 2)

MVP stuff (5)… The relationship between the mediator model and MVP

MVP stuff (6)… MVC turned into MVP

MVP stuff (7)… Repository Design analysis

The introduction

As in recent years the rapid development of mobile Internet, mobile Internet technology also get a boost, aided architecture design model framework and ideas emerge in endlessly, from 2015 to now, blowout developers are more and more inseparable from these tools of high performance, high efficiency, and companies or individuals to make these tools, also be pushed to the altar, the worship of the apes. At the same time, Google released its official framework Architecture Components at this year’s IO Conference, which is quite appropriate. There is no time to lose. Understanding architecture knowledge and flexible application is an essential skill for our programmers in the future. This series of articles introduces MVP architecture, hoping to bring some harvest to friends who read this article.

1. The basic principles of how to implement an MVP framework and the application scenarios of MVP are my original intention to write this article, which is also an overall integration of my own knowledge.

2. In order to avoid a stiff opening, I will use a simple development example as an introduction to look at the problem from a beginner’s point of view, and at the same time take into account the beginners. We will give you step-by-step tips later. So you can also choose to jump directly to…

3. Since architecture is mainly about scene description, so the amount of code is relatively small. For those who are used to understanding through code, I will try to describe as much as possible and repeatedly.

scenario

To implement a list display function for an item, we first need a user interface that contains some controls for the user to operate, such as list display. Most of the time, we use a Fragment as the carrier of the control. In the Fragment, we call the network request tool directly and wait for the callback method to be executed to refresh the UI to display the data, so there is not much code for this requirement. Increase in demand now, want to join the drop-down refresh and tensile load more, generally is to monitor the two events, process the data in the corresponding callback, a few lines of code, still write in fragments, and once again the demand increased, to join the sorting, then we process sequencing logic, the code still write in fragments, and then increase the demand, Add a jump to the detail page, maybe some business logic before the jump, then add requirements, the list view can change from palace to waterfall, need to change the control style,… Then came the need for editable list items that could be deleted and modified, while synchronizing data to the server, and animation for editing list items… At this point we look back at our fragments and we may not be able to tolerate the messy and complex logic in our fragments.

From the Angle of the functional requirements, the function is complete, the normal operation of the software, this is no problem, but I turned to look at the code is not “no problem”, in the case of not using architecture, we have no awareness of the business logic code and view code are piling up in the Activity of that role can be said to be quite a bloated, At the same time, it is not conducive to future expansion, as non-functional requirements need to be met: Activity optimization and scalability.

Let’s review the list of requirements for this scenario:

1. List display

2, list support drop-down refresh, pull up to load more

3. For a list to be sortable, there may be n conditions

Click to jump to the details page

5, the list shows the need for diversification, waterfall rheology

6. The list is editable, perhaps including CURD, while synchronizing the server

7. Add animations when editing lists

In the next section, we first address the above requirements by adding architecture, which covers the definition of models, the responsibilities of controllers, and how they are decoupled by controlling inversion. If you know enough about MVC, you can jump straight to the MVP section.