MVVM
A cart screen with a total and an add button, split into model, view and view model. We follow one tap round the loop and see why the middle part is easy to test.
In 30 seconds
MVVM is a way to organise the code behind a screen in three parts. The model holds the data and its rules. The view is what is drawn. The view model sits between them and holds what the screen should currently say, ready to show.
Data binding links controls in the view to values in the view model, so the screen updates itself when those values change and no code copies them across.
Since the view model never refers to the view, it can be tested without a screen.
MVVM, step by step
Simplified: MVC exists in several variants; this compares the common one where the controller updates the view. Binding syntax differs by framework, and a real model would also notify the view model of outside changes.
- Step 1: A screen that does everything. A cart screen shows a total and has a button to add one more item. The quick way is to put everything in the button’s click code: change the quantity, work out the total, format it and write it into the label. Data, rules and display are tangled together, and the only way to check the sum is to tap a real screen.
- Step 2: Three parts. MVVM splits the screen in three. The model holds the data and its rules: a price of 12.50 and a quantity of 2. The view is only what is drawn. Between them sits the view model: the state of the screen in ready-to-show form, such as the text “€25.00”, plus the actions the screen offers. It never refers to the view.
- Step 3: Data binding. Nobody writes code to copy values onto the screen. Instead the view declares bindings: this label shows totalText, this field shows qty, this button is enabled when canOrder is true. A binding is a standing link. Whenever the value in the view model changes, the framework updates the control by itself.
- Step 4: A tap goes round the loop. The button is bound too, to an action: tapping it calls increment on the view model. The view model tells the model to raise the quantity from 2 to 3, then recomputes its own totalText as “€37.50”. The bindings notice the changed values and refresh the label and the field. No line of code touched the screen directly.
- Step 5: Two-way binding. An input field needs a link in both directions. When the view model’s qty changes, the field shows it; when the user types 5 into the field, the binding writes 5 into qty. The view model reacts exactly as it did to the button, and the total becomes “€62.50”. This is two-way binding, and it is what makes forms short to write.
- Step 6: Testing without a screen. Because the view model never mentions the view, a test can use it with no screen at all. Starting from a quantity of 2, the test calls increment and reads totalText, just as the view would, and checks that it says “€37.50”. Such tests run in milliseconds, which is one of the main practical reasons for the pattern.
- Step 7: Compared with MVC. MVC, the older pattern, also has a model and a view, with a controller between them. The controller receives the input, updates the model and then tells the view what to show, so it holds a reference to the view. MVVM swaps the controller for a view model that knows no view, and lets bindings do the telling.
Try it yourself
Learn this first
Learn next
- Unit testingOne small function, one test: arrange, act, assert, and a slip caught at the edge. Beginner
- Signals and fine-grained reactivityValues that know who reads them, so one change updates exactly what depends on it. Intermediate
- Dependency inversion and injectionHand an object the tools it needs instead of letting it build its own. Intermediate
Spotted a mistake on this page?