MVP for Android: How to organize your presentation layer

Source: Internet
Author: User

Original

MVP for android:how to organize presentation layer

Http://antonioleiva.com/mvp-android/


President

MVP (Model-view-presenter) mode is the derivation of the famous MVC (Model-view-controller) pattern. During this time, MVP has been taken seriously in Android application development. More and more people are talking about it, But reliable and structured information is still scarce. That's why I want to use this blog to encourage this kind of discussion and to take our knowledge
Applied to the project in the best possible way.

What is an MVP?
The MVP mode allows the presentation layer to be separated from the logic so that the interface works with how we unfold it on the screen. Ideally, the MVP pattern allows the same logic to contain completely different replaceable views.
The first thing to clarify is that MVP is not an architectural pattern. It is only responsible for the presentation layer. However, it is better to use it in your architecture than to use it at all.

Why use MVP?
In Andrid, activities makes interaction and data access mechanisms tightly coupled, which raises a problem. We can find extreme examples such as cursoradapter, mixed adapters and cursors,adapters are part of the view, and cursors should be in the depths of the data access layer.
To make an application easy to extend and maintain, we need a good layering. What do we do if we don't take the same data from the database tomorrow, but from the Web service? We need to redo the entire view.
The MVP makes views independent of our data sources. We divide the application into three different layers, which makes testing more independent. Using the MVP allows us to take most of the logic away from activities so that we can test it without using instrumentation.

how to achieve MVP in Android?

How to achieve the MVP, the answer becomes very scattered at the beginning. There are a lot of MVP variants, each of which adjusts the idea of this pattern and more comfortable implementations according to their needs. The pattern changes are based primarily on the amount of responsibility we entrust to presenter.
Is the view responsible for displaying and closing the progress bar, or should it be presenter? In Action Bar, who decides which action to show? This is the beginning of a difficult discussion. I will show how I work, but I hope this article becomes a place to discuss how to apply MVP's rigorous guidelines, Because so far there is no standard way to implement it.

The presenter
Presenter is the middleman who plays the view and model. It obtains data from the model and applies the returned data to the view. But unlike strict MVC, when you interact with the view, it also determines what will happen.

The View
View is often implemented by an activity or fragment (depending on the structure of the app), and the view contains a reference to a presenter. Ideally, The presenter will be provided by a dagger-like annotation. The callout is responsible for creating the presenter object, but you don't actually do it this way. The only thing that the view does is call the presenter method whenever there is an interactive action (such as clicking the button).

The model
In a well-architected application, this model is just a gateway to the domain layer or business logic. If we use Uncle Bob clean Architecture,model It is likely to be an interaction that implements a use case. But this is another topic, I'll discuss it in a future article. It is now entirely possible to think of it as the provider of the data presented on the view.

An example
Because this explanation is a bit long, I've written an example of an MVP on GitHub. The login interface allows you to validate the data and allow access to the home page with the list returned from the model. This article does not explain any code, because it is simple, if you find it difficult to understand, I'll be writing an article explaining it in detail.
MVP Demo Address: HTTPS://GITHUB.COM/ANTONIOLG/ANDROIDMVP

Conclusion
Separating the logic from the interface is not easy, but the MVP model prevents our activities from eventually degenerating into a very coupled class that contains hundreds of or thousands of lines of code. It is essential to organize our code in large applications. Otherwise, it is impossible to maintain and extend.

MVP for Android: How to organize your presentation layer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.