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 ViewModel through Observables. View: It represents the UI of the application devoid of any Application Logic.
2. 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
3. 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.
4. 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
6. Page 06 of 20
Difference between
MVC and MVVM
Here, are the important difference between MVVM and MVC
7. Page 07 of 20
Difference between
MVP and MVVM
Here, are the important difference between MVVM and MVC
8. 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.
10. 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.
12. 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.
13. Page 13 of 20
WhatisRepositoryinAndroidâs
MVVMarchitecture?
14. 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.
15. 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.
18. 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.