MVP namely Model-View-Presenter, from the classic MVC evolved, their basic ideas have in common places: Handling Controller / Presenter responsible for logic, Model provides data, View is responsible for displaying.
First, the structure
In the MVP, View not directly communicate with the Model, but indirectly through the Presenter, i.e. all interactions happen inside Presenter, to improve:
1 |
void View :: the onKeyDown () the override |
The difference is that, MVC is a system-level architecture, MVP is on a particular page, that is much larger than the MVP flexibility MVC (as all business code are placed in the Presenter), to implement extremely simple.
Second, the advantages and disadvantages
advantage
- Complete separation of model and view, such modifications do not affect the view of the model
- More efficient use of the model, because all interaction taking place inside the Presenter
- Presenter can be used for a plurality of views, without changing the logical Presenter (ratio of change in view of the frequent changes always Model)
- Detachable user interface (View) unit test (business logic in Presenter)
Shortcoming
Because of rendering views on the Presenter, the so Presenter view and interaction would be too frequent. There is little need to understand that if too much Presenter rendering the view, often making it associated with a particular view too closely. Once the view needs to be changed, we also need to change the Presenter. For example, originally used to render the Html Presenter is now required for presenting Pdf, then the view is likely also need to change.
For details, please refer to Baidu Encyclopedia --MVP .
Third, the sample code
model
1 |
class : public AbstractModel |
view
1 |
class View : public AbstractViews |
organizer
1 |
class Presenter : public AbstractPresenter |
主程序及运行结果
1 |
int main() |
Appendix: Complete Source Package
Fourth, the study recommended
Project Introduction
Do list software (Android, MVP) , which is provided by Google for learning in which a sample project. README.md file them accordingly description is not repeated here. In addition, you can view other branches of learning how to use the framework to achieve the other application software.
Some suggestions to complete the white
- After the Clone project or download the Zip decompression, open the project using Android Studio
- Click "Make Project" under the menu bar Build, if the failure to operate the prompts until success
- Click on the menu bar under the Run "Run 'app'"
- To understand the actual operation of the software in a real or virtual Andrews Andrews Machine
- MVP framework related to code analysis
- Experience, summed up the organization, standardized way related interface code