The MVP architecture of Android

Source: Internet
Author: User

The MVP (model View Presenter) model evolved from the MVC paradigm and is becoming more and more important in today's Android program development. This article briefly discusses the idea of the MVP model.

What is MVP?

The main idea of the MVP model is to separate the business logic of the program from the presentation layer, ideally, the MVP pattern can make the same logic have completely different and interchangeable views.

Why use MVP?

In the actual Android development process, we will encounter the problem: activity in Android and interface display and data access mechanism is very high coupling, is responsible for the display of the page components, but also responsible for processing business logic. An extreme example is the hybrid adapter Cursoraapter, which is part of the view, but with a cursor, which should be placed in the data access layer.

For the application to have good extensibility and maintainability, we need to have a good architecture layered design. If we do not retrieve data from the database in the future, but get the data from the network, then we have to re-make a view out.

The MVP layered architecture separates views from data sources, separates business logic from activity, and it divides the application into at least three different levels, allowing you to debug your program independently and separately.

How to use MVP

MVP architecture is not very rigid, different developers can according to the needs of their projects, the idea of MVP to use in their own projects.

The presenter

Presenter, as the middle layer of the view and model layers, is responsible for retrieving data from the model layer and formatting it, and returning it to the view layer, based on the business logic. However, unlike traditional MVC, it is also responsible for responding to your interactions with the view.

The View

The view layer is often implemented by activity (and can also make fragment,view, etc.), and it often holds a presenter reference. Typically, the di is injected by reference, which makes it perfectly possible to make presenter available to the view layer call. (Example Dagger) The view layer has to do is to invoke the method in presenter to complete the user interface operation.

The model

The model layer is used to provide services for data completion. In a well-architected application, the model layer is simply a portal to accessing data as the business logic layer. Article:Uncle Bob Clean Architecture

?

Summarize

Separating the business logic from the interface in Android is not an easy task, with the MVP architecture in mind, we try to reduce the amount of code in the activity, which makes our programs more scalable and maintainable. A good example of the MVP on GitHub is a MVP example on GitHub

The MVP architecture of Android

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.