Android
MVVM
by Syed Asfandyar Zaidi
DevSky Private Limited
Page 02 of 20
01
What is MVVM
04
Difference B/W
MVP & MVVM
02
About MVVM
03
Difference B/W
MVC & MVVM
05
Data Binding
06
What is Repository
LIST OF C ONTENTS
07
What is Live Data
08
Examples
09
Testing MVVM
10
Discussion Session
Page 03 of 20
What Is MVVM?
MVVM architecture facilitates a separation of development of the
graphical user interface with the help of mark-up language or GUI
code. The full form of MVVM is Model–View–View Model. The
view model of MVVM is a value converter that means that it is view
model’s responsibility for exposing the data objects from the Model
in such a way that objects are easily managed and presented.
MVVM stands for Model, View, ViewModel.
Model: This holds the data of the application. It cannot directly talk to the View.
Generally, it’s recommended to expose the data to the View Model through Observables.
View: It represents the UI of the application devoid of any Application Logic. It
observes the View Model.
ViewModel: It acts as a link between the Model and the View. It’s responsible for
transforming the data from the Model. It provides data streams to the View. It also
uses hooks or callbacks to update the View. It’ll ask for the data from the Model..
Page 04 of 20
About MVVM
Page 05 of 20
About MVVM
Page 06 of 20
Difference between
MVC and MVVM
Here, are the important difference between MVVM and MVC
Page 07 of 20
Difference between
MVP and MVVM
Here, are the important difference between MVVM and MVC
Page 08 of 20
WhatisdatabindinginMVVMAndroid?
The Data Binding Library is a support library that allows you to bind
UI components in your layouts to data sources in your app using a
declarative format rather than programmatically. Layouts are often
defined in activities with code that calls UI framework methods.
Page 09 of 20
WhatisdatabindinginMVVMAndroid?
Page 10 of 20
WhatisTwo-WayDataBinding
As the name suggests it’s nothing but a binding view and model
together to get the latest updates both ways.
Page 11 of 20
WhatisTwo-WayDataBinding
Page 12 of 20
WhatisRepositoryinAndroid’s
MVVMarchitecture?
Repository is a class which purpose is to provide a clean API for accessing data.
What that means is that the Repository can gather data from different data
sources(different REST APIs, cache, local database storage) and it provides this
data to the rest of the app. The other components don’t know where the data
comes from, they just consume it. It also serves as a single source of truth. Its
role is to keep the local database up to date with the newest fetched data
from remote service so that the application can still provide its functionalities
with bad Internet connection or no connection at all.
Let’s look at an example where the ViewModel asks the Repository for data
which will be visualized in the View.
Page 13 of 20
WhatisRepositoryinAndroid’s
MVVMarchitecture?
Page 14 of 20
WhatisRepositoryinAndroid’s
MVVMarchitecture?
As we can see the Repository serves as a mediator between the ViewModel
and the sources of data. The process of returning data is shown in the
diagram. First the ViewModel asks the Repository for data. The Repository
checks what’s in the database and returns the persisted data to the
ViewModel for further visualization in the View. While the Repository is
getting data from the database, it also sends a request to the remote service
in parallel (asynchronously). When the response is back, the Repository
updates the database with the newest fetched data. Then this data is
consumed from the ViewModel which transforms it in an appropriate way and
returns it to the View.
Page 15 of 20
WhatisLiveData?
LiveData is an observable data holder class. Unlike a regular
observable, LiveData is lifecycle-aware, meaning it respects the
lifecycle of other app components, such as activities, fragments,
or services. This awareness ensures LiveData only updates app
component observers that are in an active lifecycle state.
Page 16 of 20
ViewModelCodeExample
Page 17 of 20
MainActivityCodeExample
Page 18 of 20
TestingMVVM
Testing is straightforward when you apply a good pattern. There are a few secrets to
testing MVVM, but here’s the best approaches.
Start testing the ViewModel. Test each one of the public methods, going through as
many cases as you can. Not just the obvious paths, but real edge cases that you’re sure
are never going to happen. Trust me: users and future developers will reach those edge
cases. If something isn’t testable, wrap it! Create an interface that contains all of the
methods you need from the untestable class. Create a class that implements that
interface and instantiate the untestable class you wanted to use inside of it. Then, inside
your class, call the methods from the untestable class. This is called the Proxy design
pattern. Now you have an interface you can use in your app that’s easily replaceable
with a mock object. This way, you can “throw” untestable dependencies outside your
class.
Thank You
Yo u M a y A s k Q u e s t i o n N o w
Discussion Session

MVVM Presentation.pptx

  • 1.
    Android MVVM by Syed AsfandyarZaidi DevSky Private Limited
  • 2.
    Page 02 of20 01 What is MVVM 04 Difference B/W MVP & MVVM 02 About MVVM 03 Difference B/W MVC & MVVM 05 Data Binding 06 What is Repository LIST OF C ONTENTS 07 What is Live Data 08 Examples 09 Testing MVVM 10 Discussion Session
  • 3.
    Page 03 of20 What Is MVVM? MVVM architecture facilitates a separation of development of the graphical user interface with the help of mark-up language or GUI code. The full form of MVVM is Model–View–View Model. The view model of MVVM is a value converter that means that it is view model’s responsibility for exposing the data objects from the Model in such a way that objects are easily managed and presented.
  • 4.
    MVVM stands forModel, View, ViewModel. Model: This holds the data of the application. It cannot directly talk to the View. Generally, it’s recommended to expose the data to the View Model through Observables. View: It represents the UI of the application devoid of any Application Logic. It observes the View Model. ViewModel: It acts as a link between the Model and the View. It’s responsible for transforming the data from the Model. It provides data streams to the View. It also uses hooks or callbacks to update the View. It’ll ask for the data from the Model.. Page 04 of 20 About MVVM
  • 5.
    Page 05 of20 About MVVM
  • 6.
    Page 06 of20 Difference between MVC and MVVM Here, are the important difference between MVVM and MVC
  • 7.
    Page 07 of20 Difference between MVP and MVVM Here, are the important difference between MVVM and MVC
  • 8.
    Page 08 of20 WhatisdatabindinginMVVMAndroid? The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Layouts are often defined in activities with code that calls UI framework methods.
  • 9.
    Page 09 of20 WhatisdatabindinginMVVMAndroid?
  • 10.
    Page 10 of20 WhatisTwo-WayDataBinding As the name suggests it’s nothing but a binding view and model together to get the latest updates both ways.
  • 11.
    Page 11 of20 WhatisTwo-WayDataBinding
  • 12.
    Page 12 of20 WhatisRepositoryinAndroid’s MVVMarchitecture? Repository is a class which purpose is to provide a clean API for accessing data. What that means is that the Repository can gather data from different data sources(different REST APIs, cache, local database storage) and it provides this data to the rest of the app. The other components don’t know where the data comes from, they just consume it. It also serves as a single source of truth. Its role is to keep the local database up to date with the newest fetched data from remote service so that the application can still provide its functionalities with bad Internet connection or no connection at all. Let’s look at an example where the ViewModel asks the Repository for data which will be visualized in the View.
  • 13.
    Page 13 of20 WhatisRepositoryinAndroid’s MVVMarchitecture?
  • 14.
    Page 14 of20 WhatisRepositoryinAndroid’s MVVMarchitecture? As we can see the Repository serves as a mediator between the ViewModel and the sources of data. The process of returning data is shown in the diagram. First the ViewModel asks the Repository for data. The Repository checks what’s in the database and returns the persisted data to the ViewModel for further visualization in the View. While the Repository is getting data from the database, it also sends a request to the remote service in parallel (asynchronously). When the response is back, the Repository updates the database with the newest fetched data. Then this data is consumed from the ViewModel which transforms it in an appropriate way and returns it to the View.
  • 15.
    Page 15 of20 WhatisLiveData? LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.
  • 16.
    Page 16 of20 ViewModelCodeExample
  • 17.
    Page 17 of20 MainActivityCodeExample
  • 18.
    Page 18 of20 TestingMVVM Testing is straightforward when you apply a good pattern. There are a few secrets to testing MVVM, but here’s the best approaches. Start testing the ViewModel. Test each one of the public methods, going through as many cases as you can. Not just the obvious paths, but real edge cases that you’re sure are never going to happen. Trust me: users and future developers will reach those edge cases. If something isn’t testable, wrap it! Create an interface that contains all of the methods you need from the untestable class. Create a class that implements that interface and instantiate the untestable class you wanted to use inside of it. Then, inside your class, call the methods from the untestable class. This is called the Proxy design pattern. Now you have an interface you can use in your app that’s easily replaceable with a mock object. This way, you can “throw” untestable dependencies outside your class.
  • 19.
    Thank You Yo uM a y A s k Q u e s t i o n N o w
  • 20.