美文网首页
谷歌的MVP标准

谷歌的MVP标准

作者: 媚竹风 | 来源:发表于2017-03-23 09:31 被阅读202次

MVP争论了很久,各家祭出各家的MVP模式,可谓百家争鸣,谷歌终于按耐不住了,祭出了自己的 mvp的参考标准了,实在是不容易。也终于停止mvp的各种争论了,特别是对The MVP这种浪费时间又很lower的设计真的可以不用看了。

功力又上升一个阶段了。

Github的地址:
https://github.com/googlesamples/android-architecture

前言

Android官方终于发布了自己的MVP框架自己的实现,这个还真的不容易,等了这么久,之前都是各家实现各自的mvp框架,但是大概的思想是那么一回事,只是在处理细节的时候发生了分歧。例如:

  • 关于ContentProvider如何进行处理,应该放在那一层。
  • 在有Loader的情况下该如何处理。
  • 其它的情况,如databing等等情况。

关于mvp的代码分析文章:

http://mp.weixin.qq.com/s?__biz=MzA3ODg4MDk0Ng==&mid=403539764&idx=1&sn=d30d89e6848a8e13d4da0f5639100e5f&scene=0

学习总结

我实在是不想说的太多,码字太累了,在github上,谷歌把一切说的非常的详尽了。所以不多说了。

注意的是:

  • 官方的mvp需要一个 契约类将view和presntent连接起来。

  • 官方使用的fragment,而不是在activity,因为原因很简单,首先,contentProvider和 Loader定义的都是数据加载器,也就是属于model的,Presenter 仅仅是一个主持人的角色,所以,这些数据加载器,在activity中赋值给presenter,甚至在官方的presenter中,数据加载器也就是model通过构造器传入,而不是在presenter中去new ,自我感觉这种设计方法非常的奇妙也非常的合理,那么fragment成了view,而activity则成了client:

    The separation between Activity and Fragment fits nicely with this implementation of MVP: the Activity is the overall controller that creates and connects views and presenters.

所以官方的推荐中,activity中不推荐作为 view,因为activity中本身持有context,而context中contentprovider和loader有属于model层。

A contract defining the view and the presenter

An Activity which is responsible for the creation of fragments and presenters
A Fragment which implements the view interface.

A presenter which implements the presenter interface

官方示例

在谷歌托管到github上有 6个示例,其中有三个完成的示例,三个正在进行中的示例,不过看了示例的源代码后觉得代码的水平和Android的源代码差远了。

实际上下面的几个例子,其本质是一样的。MVP ,Fragment充当View,P不持有任何的Context,Activity用来组装 P和View,或者说组成P和view的关系联系。

Basic Model-View-Presenter architecture.

image

fetches data using Loaders

image

关于 DateBinding,以前还没有接触过,不过看起来很牛逼的样子:官网上是这样的介绍的。

The Data Binding Library saves on boilerplate code allowing UI elements to be bound to a property in a data model.

  • Layout files are used to bind data to UI elements
  • Events are also bound with an action handler
  • Data can be observed and set up to be updated automatically when needed

为了更直观点,我们看看使用data bindding 的xml文件

  <TextView
                android:id="@+id/noTasksAdd"
                android:layout_width="wrap_content"
                android:layout_height="48dp"
                android:layout_gravity="center"
                android:background="@drawable/touch_feedback"
                android:gravity="center"
                android:text="@string/no_tasks_add"
                android:onClick="@{() -> actionHandler.addNewTask()}"
                android:visibility="@{tasks.tasksAddViewVisible ? View.VISIBLE : View.GONE}" />

很神奇吧。在xml中直接绑定数据。关于这个框架,在这里先不去看,等有时间或者机会在去看看这个框架。

uses Content Providers

结构和 user loader差不多,这里不多说了。

uses concepts from Clean Architecture

concepts from Clean Architecture 是一个新的概念结构

image

domain layer是一个领域层,什么是领域层?

首先我们 看看 The Clean Architecture 这个框架:

二个地址:

The Clean Architecture 框架或者说架构就是一种思想。从上面的图可以看出,它将业务层单独的分离出来。

uses Dagger2 for Dependency Injection

这个作者还没有完成。

实际上 Dagger 也是一个基于注释的框架,实际上没有什么的。

相关文章

网友评论

      本文标题:谷歌的MVP标准

      本文链接:https://www.haomeiwen.com/subject/nocznttx.html