MVP on Android: How to organize the content of the display layer

Source: Internet
Author: User

MVP (Model View Presenter) is an evolutionary version of the well-known MVC model, which is becoming more and more important in Android application development, and everyone is talking about the theory of MVP. Only the structured data is very small. That's why I'm writing this blog, and I want to encourage you to be more involved in the discussion and then apply the MVP model to project development.

What is an MVP?

MVP mode can separate the display layer and the logic layer, so the function interface how to work with the function of the display can be separated, the MVP model can ideally achieve the same logic code with different display interface. The first thing to clarify is that the MVP is not a structured model, it is only responsible for the display layer, at any time can be used in their own project structure of the MVP model.

Why use MVP?

We know that the logical interface and data access on Android are tightly coupled, and this problem can be seen in the example of CursorAdapter, which incorporates both the adapter and the display component, and the cursor is largely the data access layer.

For a scalable, stable application, we need to define the separate layers, after all, we don't know what logic to add in the future, is to retrieve the data from the local database? or from a remote Web service?

The MVP mode allows the display interface to be separated from the data, and the applications we develop can be separated at least three layers, allowing for independent testing. With the MVP we can separate most of the code from the activity, and we can test each module individually without unit testing.

How to achieve MVP on Android?

When it comes to this, the problem is a bit complicated. There are many ways to achieve the MVP, and everyone can modify the way the MVP is implemented according to their own needs and the way they like, which can vary with the complexity of the presenter.

Do I need to control the progress bar in view? Or is it handled in presenter? And who decides what action the action Bar is going to show? It was a difficult decision. I'm going to show myself here, but I want this article to be a place to discuss how to apply the MVP, because there's no standard way to achieve MVP yet.

Presenter

Presenter is primarily a bridge between view and model, which retrieves data from the model layer and returns it to the view layer, but does not want a typical MVC structure, because it can also determine the interaction with the view layer.

View

VIew is usually implemented by activity (perhaps Fragment,view, depending on the overall structure of the app), it will contain a presenter reference, ideally presenter is provided by a dependency injection manager, such as Dagger, However, if you don't use an injector, you need to create the presenter object independently. The only way to do this is to call presenter each time an interface is called, such as when a button is clicked.

Model

For a structured app, the model is primarily a channel to the main domain layer or logical layer, and if you use Uncle Bob clean architecture, the model is probably an interactive tool that implements the use case scenario. That's one of the topics I'm going to discuss in another article. Now, just think of it as a container that provides data to view.

An example

Given that it has been explained for too long, I have written an example of an MVP example on Github, consisting of a login interface that can validate the data and then enter a main interface with a list of data from model, because it is relatively simple, so this article does not talk about code, But if the reader feels that it is still difficult to understand, I can also write an article detailed introduction.

Summarize

To separate the interface and logic on Android is not easy to implement, but the Model-view-presenter mode can be more simple to prevent doping too much code in the activity in large projects, the organization of the code structure is the most basic requirements, otherwise, the code stability and expansion is very difficult.

Original link: Antonioleiva translation: Bole online-Chris
Link: http://blog.jobbole.com/71209/

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.