Android中的MVP筆記之五: MVP和MVVM的區別

谷歌關於mvvm的官方示例地址:android

https://github.com/googlesamples/android-architecture/tree/dev-todo-mvvm-databinding/git

 

MVP( Model-View-Presenter)和MVVM (Model-View-ViewModel) 的區別引用官方的話就是github

The ViewModel in MVVM is very similar to the Presenter in MVP. The main difference has to do with the communication between View and ViewModel/Presenter:mvvm

  • MVVM: when the ViewModel is modified, the View is updated by a library or framework. There's no way to update the View directly from the ViewModel, as it doesn't have this reference.
  • MVP: The Presenter has a reference to the view so when a change is necessary, the presenter calls the view to explicitly make the change.

MVP 模式在安卓開發中能夠不使用任何額外的支持庫,也不須要其餘的第三方類庫,各層之間經過組合模式相互擁有所須要的實例,經過回調接口傳遞數據和處理邏輯,以達到讓View層只專一於界面顯示,P層負責在處理邏輯並做爲View與Model的橋樑。this

MVVM模式在安卓中的實現,須要使用官方提供的android.databinding庫,固然android.databinding庫也能夠使用在MVP模式裏,MVP模式能夠結合android.databinding庫使用,也能夠不使用,但MVVP模式必定要使用android.databinding庫。google

根據官方的說明,MVP與MVVP的最大區別在於MVP模式裏,P層以擁有View層的引用,當須要更新View時,P層通知View層更新界面,而MVVP模式裏,ViewModel層再也不擁有View的引用,View的更新和數據綁定經過android.databinding自動實現。spa

相關文章
相關標籤/搜索