How to use MVP in your own projects
The official wrote us some MVP sample projects, using different concepts and tools to implement the same Todo project.
- todo-mvp/ -mvp infrastructure example.
- todo-mvp-loaders/-Based on the mvp infrastructure project, the data acquisition part uses Loaders architecture.
- todo-databinding/ -Based on the mvp infrastructure project, using data binding components.
- todo-mvp-clean/ -Based on the mvp infrastructure project, using the concept of clean architecture.
- todo-mvp-dagger/ -Based on the mvp infrastructure project, dagger2 is used for dependency injection.
- todo-mvp-contentproviders/-Based on todo-mvp-loaders architecture project, using Content Providers
- todo-mvp-rxjava/-Based on the mvp infrastructure project, the full name uses RxJava for concurrency and data layer processing.
Although before the official launch of this set of MVP open source use cases, there are also many excellent open source projects on the Internet that teach you how to use the MVP model. If you haven’t read it before, there is actually another advantage. Just follow the official As soon as I started, other similar projects were misfired). I took a look at the official todo-mvp, which is a bit simpler and clearer than what I did before, and the test cases are also written more complete, so you can intuitively experience the benefits of MVP in this regard.
Benefits and problems of MVP
When you understand the MVP model, its benefits are obvious:
- The UI layer is separated from the logic layer. The UI layer does not involve business logic code, and changes to a certain layer do not need to modify the code everywhere;
- Testing is very convenient, you can directly call the Presenter layer to write test styles (you can use the Junit framework);
- The last is maintainability and scalability. The responsibilities of each category of MVP are very clear and single. Later expansion and maintenance will be easier.
Of course, the disadvantages are also obvious. First of all, the code classes are increased. You may have to write the implementation of the Presenter and Model layers for a small function. In the past, you added these to the View layer in one go. At the same time, some MVP mode training is needed for new project personnel so that they will not write code that destroys the existing mode.