These patterns define how your app separates data, UI, and logic, improving maintainability, scalability, and testability.
π― 1οΈβ£ ModelβViewβController (MVC)
π Definition:
MVC divides your app into Model (data), View (UI), and Controller (input logic).
The Controller updates the Model, and the View observes the Model for changes.
βββββββββββββββ user input βββββββββββββββ
β View β ββββββββββββββββββββββββΆ β Controller β
ββββββββ¬βββββββ ββββββββ¬βββββββ
β updates / notifies β
β β updates model
βΌ βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Model β
β (business logic, data management, domain state) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β²
β notifies changes
β
ββββββββ΄βββββββ
β View β
βββββββββββββββ
Flow Summary:
User β Controller β Model β View β User
β Pros
- Good separation of concerns
- Multiple views can share the same model
- Controller logic is easy to test
β οΈ Cons
- Controller and View can become tightly coupled
- Not ideal for very large UIs
π― 2οΈβ£ ModelβViewβPresenter (MVP)
π Definition:
MVP replaces the Controller with a Presenter, which handles all presentation logic.
The View is passive β it only displays data sent by the Presenter.
βββββββββββββββ
β View β ββββββββββββββββ
β (UI layer) β β
ββββββββ¬βββββββ β
β calls β updates
βΌ β
βββββββββββββββ β
β Presenter β ββββββββββββββββ
β (presentation logic) β
ββββββββ¬βββββββ
β interacts with
βΌ
βββββββββββββββ
β Model β
βββββββββββββββ
Flow Summary:
User β View β Presenter β Model β Presenter β View
β Pros
- Great testability (Presenter independent of UI)
- Loose coupling between View and Model
- Well-suited for event-driven UIs
β οΈ Cons
- Presenter can grow large ("god object")
- More boilerplate
π― 3οΈβ£ ModelβViewβViewModel (MVVM)
π Definition:
MVVM introduces a ViewModel, an abstraction of the View that handles all UI state and supports two-way data binding with the View.
βββββββββββββββββ
β Model β
β (Data logic) β
βββββββββ¬ββββββββ
β
β updates / observes
βΌ
βββββββββββββββββ
β ViewModel β
β (State + logicβ
β for View) β
βββββββββ¬ββββββββ
β² β²
two-way binding β β events / user actions
β β
βββββββββββββββββ
β View β
β (UI markup) β
βββββββββββββββββ
Flow Summary:
Model β ViewModel β View
β Pros
- Automatic two-way data binding
- Very high testability (ViewModel is logic-only)
- Ideal for reactive UIs (Vue, Angular, Knockout, React+MobX)
β οΈ Cons
- Harder to debug (implicit bindings)
- ViewModel can grow large if not modularized
- Slight binding performance overhead
βοΈ Comparison Table
Aspect / Pattern | MVC | MVP | MVVM |
---|---|---|---|
Main Components | Model, View, Controller | Model, View, Presenter | Model, View, ViewModel |
Flow Direction | Controller β Model β View | Presenter β Model β View | ViewModel β View, ViewModel β Model |
View Role | Active (observes Model) | Passive (updated by Presenter) | Passive (bound to ViewModel) |
Data Binding | Manual / One-way | Manual / One-way | Automatic / Two-way |
Testability | Medium | High (Presenter easily tested) | High (ViewModel logic testable) |
Coupling | Tight ViewβController link | Looser ViewβPresenter link | Loosest (View β ViewModel via binding) |
Best Use Case | Simpler apps, server-side MVC | Desktop / mobile UI apps | Reactive front-end apps |
Examples (JS) | Backbone.js, Express.js | Android, GWT, early JS UIs | Knockout, Vue, React + MobX |
π¬ In One Sentence
MVC splits data, UI, and control logic; MVP delegates UI handling to a testable Presenter; and MVVM binds data reactively between the View and Model via a ViewModel β ideal for modern, reactive frameworks.
Top comments (0)