简体   繁体   中英

What exactly is a view-model?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM