The Model-View-ViewModel (MVVM) pattern
Pattern · Chapter 7
Overview
Section titled “Overview”- Shares the separation of concerns goal with MVC and MVP, making apps easier to maintain, extend, and test.
- Distinctive feature: heavy view↔ViewModel interaction via data binding.
- Works well for rich desktop apps (also web and mobile). Example framework: Windows Presentation Foundation (WPF).
Components
Section titled “Components”| Component | Responsibility |
|---|---|
| Model | Business domain object and data; uses the database. May bind directly to view/ViewModel properties, so it commonly raises property-changed notifications. |
| View | The UI; the part users see. Active — unlike passive MVC/MVP views, it is aware of the model and ViewModel. Handles its own events but holds no state; relays user actions to the ViewModel (via data binding or commands). Goal: minimize code in the view. |
| ViewModel | Analogous to the controller/presenter — coordinates view and model. Provides data to views for display/manipulation, handles user actions and input, and holds navigation logic to move between views. Communicates with views via data binding, commands, method calls, properties, and events. |
Key distinctions
Section titled “Key distinctions”- The view is active and knows the model (unlike MVC/MVP passive views).
- The tight view↔ViewModel connection is realized through data binding.
Citations
Section titled “Citations”- Software Architect’s Handbook (Packt, 2018), Ch.7 “The Model-View-ViewModel pattern”, pp. 547-550.