Skip to content

MVC Pattern

MVC Pattern

Model-View-Controller — separating UI concerns from business logic

1. Overview: MVC, a pattern that splits UI, logic, and data into three roles for independent development

    flowchart LR
    A["UI, business logic, and data<br/>mixed into one codebase —<br/>changes affect everything"] --"Separate roles into<br/>Model, View, Controller"--> B["Separation of concerns,<br/>independent development and testing"] --"Replace the UI,<br/>reuse business logic"--> C["Improved maintainability<br/>and reusability"]

    style A fill:#FFEBEE,stroke:#D32F2F,color:#000
    style B fill:#E3F2FD,stroke:#1976D2,color:#000
    style C fill:#E8F5E9,stroke:#388E3C,color:#000
  

Definition: A software architecture pattern that splits an application into three roles — Model (data and business logic), View (user interface), and Controller (request handling and flow control) — so each component can be developed, changed, and tested independently.

Characteristics: (General-purpose UI pattern) Originated in Smalltalk in the 1970s and is applied broadly across web, mobile, and desktop applications as a general-purpose UI pattern. (Adopted by major frameworks) The foundational design pattern behind major web frameworks such as Spring MVC, Django, Rails, and ASP.NET. (Observer pattern) View and Model can communicate directly, with the View observing Model changes (Observer pattern).


2. Core Structure of the MVC Pattern

A. Separating the Model, View, and Controller Roles

    flowchart TD
    USER["User"]

    subgraph MVC["MVC Components"]
        direction TB
        CTR["Controller<br/>receives requests, controls flow,<br/>calls Model, selects View"]
        MOD["Model<br/>business logic and data,<br/>state management, DB integration"]
        VIW["View<br/>user interface,<br/>renders Model data"]
    end

    USER -->|"Request"| CTR
    CTR -->|"business processing request"| MOD
    MOD -->|"returns data/state"| CTR
    CTR -->|"rendering instruction"| VIW
    MOD -->|"state-change notification<br/>(Observer)"| VIW
    VIW -->|"Response"| USER

    style CTR fill:#E3F2FD,stroke:#1976D2,color:#000
    style MOD fill:#1E3A5F,stroke:#1E3A5F,color:#fff
    style VIW fill:#E8F5E9,stroke:#388E3C,color:#000
  
ComponentKey ResponsibilityIncluded ElementsChange Triggers
ModelApplies business rules, manages data state, integrates with the DBEntity, Repository, Service, DTOChanges in business rules or data structure
ViewVisually presents data to the userHTML Template, JSP, Thymeleaf, ReactChanges in UI design or screen layout
ControllerReceives requests, validates parameters, calls Model, selects View@Controller, @RestControllerChanges in request flow or routing

MVC Request Processing Flow (Spring MVC Example)

StepHandlerDescription
1DispatcherServletEntry point for all HTTP requests (Front Controller)
2HandlerMappingFinds the Controller method matching the URL pattern
3ControllerCalls business logic, returns a ModelAndView
4ViewResolverResolves the logical view name to an actual View (Template)
5ViewRenders Model data as HTML/JSON and responds

B. MVC Variant Patterns (MVP, MVVM)

    flowchart TD
    subgraph R1[" "]
        direction LR
        MVC2["MVC<br/>Model-View-Controller<br/>Controller coordinates View and Model<br/>View can reference Model directly<br/>Spring MVC / Django / Rails"]
        MVP["MVP<br/>Model-View-Presenter<br/>Presenter fully mediates View and Model<br/>View only references the Presenter<br/>Android (classic) / WinForms"]
    end
    subgraph R2[" "]
        direction LR
        MVVM["MVVM<br/>Model-View-ViewModel<br/>ViewModel manages View state<br/>auto-sync via data binding<br/>Vue / React / Angular / WPF"]
        FLUX["Flux / Redux<br/>unidirectional data flow<br/>Action → Dispatcher → Store → View<br/>state management in the React ecosystem"]
    end

    style MVC2 fill:#E3F2FD,stroke:#1976D2,color:#000
    style MVP  fill:#F3E5F5,stroke:#7B1FA2,color:#000
    style MVVM fill:#FFF3E0,stroke:#F57C00,color:#000
    style FLUX fill:#E8F5E9,stroke:#388E3C,color:#000
    style R1 fill:none,stroke:none
    style R2 fill:none,stroke:none
  

MVC vs. MVP vs. MVVM

ComparisonMVCMVPMVVM
MediatorControllerPresenterViewModel
View–Model relationshipView can reference Model directlyPresenter fully mediatesAuto-synced via data binding
TestabilityController unit tests are straightforwardPresenter unit tests are straightforwardViewModel unit tests are straightforward
View independenceModerateHigh (View is only an interface)High (decoupled via binding)
Suitable environmentServer-side rendered webAndroid, desktop appsClient-side SPA, WPF
Representative frameworksSpring MVC, Django, RailsAndroid MVP, WinFormsVue, Angular, WPF, Kotlin

3. Expected Benefits and Application of the MVC Pattern

CategoryKey Expected BenefitApplication and Practical Use
Separation of concernsUI changes don’t affect business logicParallel development by designers (View), backend engineers (Model), and full-stack engineers (Controller)
TestabilityModel can be unit-tested independently of the UIUnit-test Service/Repository with JUnit, using mock Views
ReusabilityThe same Model serves Web, API, and mobile viewsReuse the Model for REST API design, swapping only the View
MaintainabilityScreen changes touch only the View; logic changes touch only the ModelGradual migration by replacing the View when modernizing legacy UI