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

相关文章
相关标签/搜索