DEV Community

ROHIT KUMAR
ROHIT KUMAR

Posted on

Architectural Patterns - MVC | MVP | MVVM

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     β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

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     β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

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)   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Enter fullscreen mode Exit fullscreen mode

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)