Analysis of Android in the MVC, MVP design pattern

MVC

 

 

        MVC refers Modle, View and a Controller, separate interface, business logic, and a controller, a low coupling design approach for simple application development. Here is a simple example. android in a variety of controls, is the View. For example, a Button. If Button is used to obtain the server information, we can achieve specific functions packaged as a feature class called HttpUtil. The acquired server information stored in ServerResponse class. So, HttpUtil and ServerResponse is the Modle. So who is the Controller of it? Activity is the Controller, it Modle View and connect. Click Button, will trigger onClickLisener method Activity, which further triggered HttpUtil class. After the acquisition result HttpUtil, packaged as ServerResponse, ServerResponse to display the appropriate control.

MVP

 

       P is a Presenter, i.e. implementor, similar functions to the Controller. For the Presenter substantial use Interface type, for reducing the coupling between M, V, C, or Fragemnt Activity gist is thin. After the Activity or Fragment thin, it should contain only setListener, life cycle control, call the View interface, init and the Presenter. And specific operations are implemented in the Presenter Implimentation class.

Here a simple example to further understanding of the design pattern MVC MVP :

If do not have any framework to achieve this requirement, it is necessary to achieve the following logic:

When using the MVC design pattern, the need to implement the following logic:

 

 

The use of MVP design pattern, the need to implement the following logic:

 

 

Respectively MVC, MVP design pattern demand through we will find the advantages and disadvantages of these two design patterns :

MVC advantages: to achieve a separation of the Model View to some extent, to reduce the coupling procedure.

MVC缺点:Controller与View难以完全解耦,随着项目复杂度的提升,Controller将越来越臃肿。

MVP优点:解决了MVCModl与View过度耦合的缺点,职责划分明显,更加易于维护。

MVP缺点:接口数量多,项目复杂度升高,随着项目复杂的的提升,Presenter层将越来越臃肿。

 

使用MVP的建议:

(1)接口规范化(封装父类接口以减少接口的使用量)

(2)使用第三方插件自动生成MVP代码

(3)对于一些简单的界面,可以选择不适用框架。

(4)根据项目复杂程度,部分模块可以选择不使用接口。

 

最后总结一下MVP和MVC的差别:

(1)Model与View不再直接通信,而是通过中间层Presenter来实现。

(2)Activity的功能被简化,不再充当控制器,主要负责View层面的工作。

 

 

发布了9 篇原创文章 · 获赞 11 · 访问量 227

Guess you like

Origin blog.csdn.net/Healer_LU/article/details/104231545