MVVM with RxJava in Android

In my last tutorial, we have learned the MVP design pattern which is most popular to used to build any android application. In that article, we had practiced building an application by using RxJava in MVP design pattern. If you are not checked my last articled which is based on MVP design pattern then I would recommend checking this here MVP with RxJava in android. Wow, that's great.Today We got one more day lets make this day more awesome to learn something new. In this article, we will learn one more design pattern which comes from data binding in android that is called MVVM (Model View ViewModel). Model view ViewModel is similar like MVP.

Here we need to understand the what are the aspects in this diagram. Let me explain one by one.Model: Model represents the classes that used for creating the business model of an application. It means how the data can manipulate.View: View represents the user interface means Button or TextView. View display the data from the model.The view which binds with Observable variables and action triggered for ViewModel.ViewModel: ViewModel is a middle layer for interaction between the View and model. Its responsibility is preparing the Observable data which is needed to view and also hooks for model event triggered from view.This pattern is using the data binding concept which means that multiple views contain with single ViewModel. ViewModel should have all information which is required for the view. In this case, ViewModel that can be reused for multiple views. For Single ViewModel interaction for a module, our debug and unit testing becomes easy. A developer can find the issue easily and it makes easy to add any new feature in existing code. Overall it makes our life pretty easy and simple.Let's create an android project by using RxJava in MVVM design pattern. The code structure will be exactly same as we have shown in MVP. Please check this structure for MVVM.

Here I am using the green dao ORM for a database. In this project, I am trying store articles information and fetch to display those articles on recyclerView. Let's. Lets create the base interface and BaseFragment class.

Here in future, if required to store this articles information on the server and fetch those article from the server and save on locally. Then It will not require much code changes. We just need to look into ViewModel class rather than wasting any time on view or model class. It is a separate class that makes debug and unit testing becomes easy for the developer.Thanks for reading this post.