Simple Architecture--android MVP ...

Source: Internet
Author: User

The use of MVP in Android has actually been popular for a long time, including our company, through our Android side of the thinking and talent of our products are also taking the MVP model.
Today, I would like to share my humble opinion on the MVP and how to use the MVP model to build a project framework.
?? Description: Due to my ability and limited time, so this article is only a point of omission, please understand.
The usual, first:

MVP Overview MVP Definition

MVP, full name Model-view-presenter, is an architectural model;
Evolved from the old-timer of MVC;
As for the difference between MVC,MVP,MVVM, see: MVC,MVP and MVVM illustrations.

Simple knowledge:

Roughly divided into Model, View, presenter three layers, the following describes the role of these layers:

    1. View: Responsible for drawing UI elements, interacting with the user (embodied as activity in Android);
    2. View interface: An interface that requires a view implementation, the view interacts with the presenter through the view interface to reduce coupling and facilitate unit testing;
    3. Model: Responsible for storing, retrieving, manipulating data (sometimes also implementing a model interface to reduce coupling);
    4. Presenter: As the intermediate link between view and model, handles the responsible logic for interacting with the user.
MVP Considerations:

Benefits of MVP:

    1. The view logic and business logic are separated, and the coupling is reduced;
    2. Activity only handles the life cycle tasks, and the code becomes more concise;
    3. We can use a presenter for multiple views without changing the logic of presenter. This feature is very useful because the change in view is always more frequent than the model;
    4. The view logic and business logic are abstracted into the interface of view and presenter, which improves the readability of the Code;
    5. Presenter is abstracted into an interface, can have a variety of concrete implementation, so it is convenient for unit testing;
    6. The business logic is pumped into the presenter, preventing the background thread from referencing the activity causing the resource of the activity not to be reclaimed by the system causing memory leaks and oom

Insufficient:

    1. Presenter in addition to the application logic, there are a large number of View->model,model->view manual synchronization logic, resulting in presenter heavier, maintenance will be more difficult.
    2. Additional code complexity and learning costs.
MVP Practice

General ideas on the background

Background:

    1. There are three main products;
    2. The product needs constant iteration;
    3. many function modules;
    4. Develop team-based units;

General Idea:
Based on the above background, our project is split into two packages: commonlibraryapp(Presentation)

    • commonlibrary: Responsible for the provision of tools and management of third-party libraries, and business logic is completely unrelated, can be used across projects;
    • Presentation: Responsible for displaying graphical interface, filling data, processing business logic;

    • Presentation The module according to function:

Package Structure Division

Whether a project is well-expanded and flexible enough, the package structure is divided into a large proportion. Many projects like to use according to the characteristics of sub-package (that is, activity, service, etc. are placed under a package), the module is small, the page is not a lot of time this does not have any problems, but for more modules, team development projects, this will be very inconvenient. So, my advice is to divide the package structure by module.

Res Partitioning
    • layoutLayout file divided by module
    • Other resource files as usual

Note: You need to modify the build.gradle file under module

MVP Layering Description

This layer does not prepare the subdivision, if the subdivision can refer to this article-"field-driven design series of articles-analysis of Vo, DTO, do, PO concept, difference and usefulness"

View Layer
    1. The simple page uses activity/fragment directly as the implementation class of the View, then extracts the corresponding interface;
    2. In some tabbed pages, you can use Activity + Fragment (+ Viewpager) to achieve, as for the Viewpager, depending on the situation, of course, can also be directly Activity + Viewpager or other combination of ways
    3. In some complex pages that contain many controls, it is recommended to split the interface and extract the custom view, that is, a activity/fragment contains multiple view (implementation of multiple view interfaces)
Presentation

For program logic processing, interface and view interaction, decoupling business and interface

This side will be a large number of View->model,model->view manual synchronization logic, resulting in presenter relatively heavy, maintenance will be more difficult, so here we use the Eventbus to decouple

about the selection of third-party libraries

Please refer to: How to evaluate whether an open source library is worth introducing!!!

MVP mode of the Open Source Library:

    • Sister paper. Gank.io
    • Mr. Bun
    • Gankapp

Reference:

    • http://www.jianshu.com/p/0590f530c617#
    • http://blog.chengdazhi.com/index.php/115
    • http://www.jianshu.com/p/9a6845b26856
    • http://blog.chengdazhi.com/index.php/131
    • Http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html

Simple Architecture--android MVP ...

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.