谷歌的MVP標(biāo)準(zhǔn)

MVP爭論了很久,各家祭出各家的MVP模式,可謂百家爭鳴,谷歌終于按耐不住了,祭出了自己的 mvp的參考標(biāo)準(zhǔn)了,實在是不容易。也終于停止mvp的各種爭論了,特別是對The MVP這種浪費時間又很lower的設(shè)計真的可以不用看了。

功力又上升一個階段了。

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

前言

Android官方終于發(fā)布了自己的MVP框架自己的實現(xiàn),這個還真的不容易,等了這么久,之前都是各家實現(xiàn)各自的mvp框架,但是大概的思想是那么一回事,只是在處理細節(jié)的時候發(fā)生了分歧。例如:

  • 關(guān)于ContentProvider如何進行處理,應(yīng)該放在那一層。
  • 在有Loader的情況下該如何處理。
  • 其它的情況,如databing等等情況。

關(guān)于mvp的代碼分析文章:

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

學(xué)習(xí)總結(jié)

我實在是不想說的太多,碼字太累了,在github上,谷歌把一切說的非常的詳盡了。所以不多說了。

注意的是:

  • 官方的mvp需要一個 契約類將view和presntent連接起來。

  • 官方使用的fragment,而不是在activity,因為原因很簡單,首先,contentProvider和 Loader定義的都是數(shù)據(jù)加載器,也就是屬于model的,Presenter 僅僅是一個主持人的角色,所以,這些數(shù)據(jù)加載器,在activity中賦值給presenter,甚至在官方的presenter中,數(shù)據(jù)加載器也就是model通過構(gòu)造器傳入,而不是在presenter中去new ,自我感覺這種設(shè)計方法非常的奇妙也非常的合理,那么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的源代碼差遠了。

實際上下面的幾個例子,其本質(zhì)是一樣的。MVP ,F(xiàn)ragment充當(dāng)View,P不持有任何的Context,Activity用來組裝 P和View,或者說組成P和view的關(guān)系聯(lián)系。

Basic Model-View-Presenter architecture.

image

fetches data using Loaders

image
image

在圖中,Loader 就不用說了,系統(tǒng)自帶的有一個 CursorLoader。在Presenter中,LoaderManger作為參數(shù)傳入的,在MVP,P是不持有任何context對象的。所以 LoderManger的創(chuàng)建不能發(fā)生在P中。

實際上,在谷歌發(fā)布的這一款 MVP中,P持有model,但是model的創(chuàng)建不是P中進行的,而是在P外進行的,而Model的創(chuàng)建和 P與View建立的聯(lián)系是在activity中進行的,fragment作為view存在,activity更多的是作為組裝使用。

使用Fragment作為 View的原因:

  • 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.
  • Tablet layout or screens with multiple views take advantage of the Fragments framework.

uses the Data Binding Library

image

關(guān)于 DateBinding,以前還沒有接觸過,不過看起來很牛逼的樣子:官網(wǎng)上是這樣的介紹的。

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中直接綁定數(shù)據(jù)。關(guān)于這個框架,在這里先不去看,等有時間或者機會在去看看這個框架。

uses Content Providers

結(jié)構(gòu)和 user loader差不多,這里不多說了。

uses concepts from Clean Architecture

concepts from Clean Architecture 是一個新的概念結(jié)構(gòu)

image

domain layer是一個領(lǐng)域?qū)樱裁词穷I(lǐng)域?qū)樱?/p>

首先我們 看看 The Clean Architecture 這個框架:

二個地址:

The Clean Architecture 框架或者說架構(gòu)就是一種思想。從上面的圖可以看出,它將業(yè)務(wù)層單獨的分離出來。

uses Dagger2 for Dependency Injection

這個作者還沒有完成。

實際上 Dagger 也是一個基于注釋的框架,實際上沒有什么的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容