126
architectural behavioral

MVVM

Reference Wikipedia ↗
MVVM — class diagram
Plate 126 class diagram

MVVM (Model-View-ViewModel) is an architectural pattern that facilitates the separation of an application’s concerns – data presentation (View), interaction logic (ViewModel), and data management (Model). It’s primarily used in conjunction with data binding, allowing automatic synchronization of data between the ViewModel and the View. This reduces boilerplate code and enhances testability.

The pattern aims to create a cleaner coding separation and to simplify functional testing. The ViewModel exposes data needed by the View, and commands/methods the View can bind to for actions. The View remains passive, delegating interaction handling to the ViewModel. Changes to the Model are reflected in the ViewModel, and consequently in the View, through data binding.

Usage

MVVM is especially prominent in modern UI frameworks, providing a structured approach to building maintainable and scalable applications. It’s frequently used in:

  • Desktop Applications: WPF, UWP, and other .NET-based desktop frameworks.
  • Mobile Applications: iOS (using SwiftUI or UIKit), Android (using Jetpack Compose or Data Binding), and cross-platform frameworks like Xamarin and React Native.
  • Web Applications: While sometimes debated, MVVM principles are employed through libraries like Vue.js, Angular, and Knockout.js.
  • Any UI-driven application where a clear separation of concerns is desired for increased maintainability and testability.

Examples

  1. WPF (Windows Presentation Foundation): In WPF, developers commonly implement MVVM using data binding features. A UserControl (the View) binds its UI elements (textboxes, labels, buttons) to properties and commands exposed by a ViewModel. The ViewModel retrieves data from a Model (e.g., a database entity) and transforms it for display. For example, a simple form for editing user data would have a View representing the form, a ViewModel containing the user data and validation logic, and a Model representing the User class itself.

  2. SwiftUI (Apple’s UI framework): SwiftUI heavily leverages the MVVM pattern alongside its @ObservedObject and @State property wrappers for automatic UI updates. A View observes properties within a ViewModel and updates its display when those properties change. Interactions from the UI trigger methods on the ViewModel, which then interacts with the Model. For instance, an iOS app displaying a list of tasks could use a TaskListViewModel to manage the task data fetched from a Task model, and the SwiftUI List view would bind to the tasks array in the ViewModel.

Specimens

awaiting submission

No language specimens have been submitted for this pattern yet. Contribute one.