What exactly is a view-model and in a View-ViewModel-Model pattern?
What does the word 'model' refer to? Is the 'model' a task that has been modelled using code - for example, getting a random number and displaying it ?
And what is the significance of 'model' in ViewModel ? Does it model the visual presentation to the user like a document object model ? Any insights most welcome. Thankyou.
I will try to explain it in short in order to get the idea:
ViewModel is a class that survives orientation changes: For example look at below image:
Imagine in case you had only mainActivity
and you rotated the phone every variable would be initialized again and you would loose everything in case you did not take any action.
The other main reason is seperation of concerns
. This means that each class does one think. In our simple case scenario MainActivity
would be used for UI interactivity for example button click listeners and ViewModel
would be used for any calculations
etc.
So View-ViewModel-Model
is all about seperations of concerns, and writting better code which is clean and better to maintain!
For more information it is better to take your time and read from the source: ViewModel Overview