Simple example of Android MVP

This article is contributed by "Student Wu Xiaolong". In fact, MVP has been mentioned a lot. Some of my readers may not understand it. In fact, no amount of theory can compare to a simple practice. This article is a simple request. Weather feature to demonstrate how Android MVP is used. You can click "read the original text" to the original blog!

Effect preview

Prepare

MVP process

illustrate:

Step 1: UI implements the View method, referencing the Presenter

Step 2: The Presenter calls the Model and follows the Model's specific logic

Step 3: Model logic implementation, callback Presenter method

Step 4: The Presenter calls back the View, that is, returns to the UI, and calls back the View method

gradle file

compile 'com.loopj.android:android-async-http:1.4.9'

Description: Request the network to use async-http

Directory Structure

V of MVP

MainView.java

MainActivity

P of MVP

MainPresenter.java

Presenter

IMainPresenter

M of MVP

MainModel

MainModelBean

Source address

https://github.com/WuXiaolong/AndroidMVPSample

Summarize

MVC pattern

View (View): User interface.

Controller (Controller): business logic

Model (Model): data storage

View sends commands to Controller

After the Controller completes the business logic, the Model is required to change the state

Model sends new data to View and user gets feedback

MVP pattern

When using MVP, Activity and Fragment become the View layer in the MVC mode, and the Presenter is equivalent to the Controller layer in the MVC mode, handling business logic. Each Activity has a corresponding presenter to process the data and get the model.

MVVM pattern

将 Presenter 改名为 ViewModel,基本上与 MVP 模式完全一致。唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反映在 ViewModel,反之亦然。

上述都是转载,身为一个菜鸟,我对上述的理解是,MainModelBean数据容器,MainModel请求数据并传递数据给IMainPresenterMainPresenter实现接口IMainPresenter,Presenter并把数据传递给MainView。最后MainActivity实现MainView接口更新UI。逻辑更加清晰,代码也可以服用到其他地方。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324808495&siteId=291194637