Recently, I have been using my spare time at work to learn various network open source projects. I am also building an Android open source framework. I hope to give a summary of the knowledge. Here we use a simple application to explain MVP. There are also many github source codes at the end, which are all classic examples that you can learn from. (1). Introduction to MVP Model I believe everyone is familiar with MVC: M-Model, V-View, C-Controller. MVP is an evolved version of MVC, so the corresponding meaning of MVP is: M-Model, V-View, P-Presenter. From the combination of MVC and MVP, Controller/Presenter plays the role of logical control processing in MVC/MVP, and plays the role of controlling various business processes. The biggest difference between MVP and MVC is that M and V are not directly related, that is, there is no direct relationship between Model and View. The Presenter layer is between the two, which is responsible for regulating the indirect interaction between View and Model. In Android, it is very important that the operation of UI basically needs to be performed asynchronously, that is, the UI can only be operated in the MainThread, so it is reasonable to cut off and separate View and Model. In addition, the interaction between Presenter and View and Model can further achieve loose coupling by using interfaces to define interactive operations, and unit testing can be more conveniently performed through interfaces. So there is this picture (comparison between MVP and MVC) Comparison between MVP and MVC In fact, the most obvious difference is that MVC allows Model and View to interact, while in MVP, it is obvious that the interaction between Model and View is completed by Presenter. Another point is that the interaction between Presenter and View is through the interface (which will be reflected in the code). (2) Application of MVP model 2.1 Model layer description and specific code Provide the data we want to display in the view layer and the implementation of specific login business logic processing,
2.2 View layer description and specific code Responsible for displaying data, providing a friendly interface for user interaction. Activity, Fragment and View subclasses under MVP are reflected in this layer. Activity generally loads UI views, sets listeners and then hands them over to Presenter for processing, so it also needs to hold a reference to the corresponding Presenter. What this layer needs to do is to call the relevant methods of the presenter every time there is a corresponding interaction. (For example, button click)
2.3 Presenter layer description and specific code Presenter plays the role of the middle layer between view and model. It builds the view layer after obtaining the data of the model layer; it can also distribute the processing logic after receiving the feedback command on the view layer UI and hand it over to the model layer for business operations. It can also determine various operations of the view layer.
2.4 Login callback interface
The code flow of the demo is as follows: (Please refer to the class diagram below) 1 Activity does some UI initialization and needs to instantiate the corresponding LoginPresenter reference and implement the LoginView interface to monitor interface actions 2 After the login button is pressed, the login event is received. In onClick, it is passed to LoginPresenter for processing through the reference of LoginPresenter. LoginPresenter receives the login logic and knows that it is time to log in. 3 Then LoginPresenter displays the progress bar and passes the logic to our Model, which is the LoginModel here (LoginModelImpl, the implementation class of LoginModel), and at the same time passes OnLoginFinishedListener, which is LoginPresenter itself, to our Model (LoginModel). 4 After LoginModel finishes processing the logic, the result is notified to LoginPresenter through OnLoginFinishedListener callback 5 LoginPresenter returns the result to the view layer's Activity, and the activity displays the result Please refer to this class diagram: Class diagram of this project (3) Note: 3.1 There is also an OnLoginFinishedListener in the presenter, which is implemented in the Presenter layer. It calls back the Model layer and changes the state of the View layer to ensure that the Model layer does not directly operate the View layer. 3.2 In a good architecture, the model layer may only be an entrance to the domain layer and the business logic layer. If we refer to the popular Uncle Bob clean architecture on the Internet, the model layer may be an interactor that implements business use cases. This aspect should be covered in subsequent articles, but the current capabilities are limited. (4)MVP Classic Reference Materials Please refer to the article directly, there are a lot of learning materials about the MVP mode:
The src code of androidmvp is divided into two modules: login and main For simple operation, this project only adds the login module The github address of this project: https://github.com/CameloeAnthony/AndroidMVPDemo |
<<: Summary of Common Methods for Custom Controls
>>: Android immersive status bar implementation
Tiguan, once the leading model of joint venture S...
On August 25, the B-class pioneer hunting SUV BYD...
Promoters should all have a feeling that the budg...
Recently, Liu Genghong’s live broadcast has becom...
[[418350]] IT Home reported on August 18 that tod...
Fear, excitement, novelty and confusion - what ki...
According to the "2019 Internet Service Indu...
Regarding mobile promotion , we always say that t...
2015 is aptly called the first year of Chinese TV...
On October 10, 2018, the second round of recruitm...
In 2007, 44% of Harvard graduates entered the fin...
After nearly two years of release and various twi...
Compared with C-end products that have made great...
The blockchain game "Eden" makes money ...