MVVM
Modelo–Vista–ViewModel
Escrito por Darjus Kazakevic · revisado el
The view binds to state, instead of being told what to show.
In MVP the presenter pushes: it calls showTotal after every change, and one forgotten call is a bug. MVVM removes the push. The view-model exposes observable state, the view declares what it shows from that state, and a binding updates the screen when the state changes.
This is the shape of WPF, Android Jetpack (ViewModel with StateFlow), SwiftUI with observable objects, and Vue. The typical bug changes too. It is no longer “we forgot to refresh”, but the same value stored twice and drifting out of step.
En esencia — A view-model exposes observable state and commands; the view binds to them and updates itself, so no code has to call “refresh”.
Cuándo recurrir a él
Recurre a él cuando
- The UI framework binds data: SwiftUI, Jetpack Compose, WPF, Vue
- Screens show a lot of derived state from a little stored state
- You want screen logic testable without rendering the screen
Reconsidéralo cuando
- The toolkit has no binding, and you would have to build one first
- Server-rendered pages with no client-side state to bind
- Tiny screens where a view-model would only copy one value
The parts
- View-model
- A plain class that holds a screen's state and commands (add, checkout). It knows nothing about the view: no widget types, no layout. So a unit test can drive it directly.
- Observable state
- Values the view can subscribe to, such as StateFlow on Android or an observable object in SwiftUI. When one changes, every part of the screen bound to it updates.
- Binding
- The link between the view and the state, declared once in the view. The framework, not your code, keeps the two in step.
- Derived state
- Values computed from other state, such as a total from cart lines. Computing them instead of storing them means they cannot go stale.
Reglas de diseño
- Store the source, derive the rest
- Store the smallest state and compute totals and flags from it. A stored total can disagree with its lines; a computed one cannot.
- Expose commands named for intent
- The view calls add() or checkout(); it never sets fields directly. Validation and error state stay in the view-model, where tests can reach them.
- Never reference the view from the view-model
- No Activity, Context or widget types inside it. A view-model that holds a view leaks memory when the screen closes, and cannot be tested alone.
- Expose one state object per screen
- Publish a single UI state value (loading, content or error) instead of five separate flags. One value cannot show a spinner and an error at the same time by mistake.
Errores habituales
- Storing derived values (total, isValid) and forgetting to update one. The screen shows a total that does not match the lines.
- Passing an Activity or Context into the view-model. Memory leaks after every rotation.
- Separate isLoading, hasError and data flags. The screen shows a spinner and an error at once.
- Business rules in the view-model instead of the model, so two screens enforce the same rule differently.