Skip to main content

Model View Controller (MVC) pattern

Short answer

MVC separates an application into Model (data/business logic), View (presentation), and Controller (handles requests). The controller receives input, updates the model, and selects a view to render the response.

Explain Model View Controller (MVC) pattern.

Model-View-Controller (MVC) is a software architecture pattern that divides an application into three connected components so that data, user interface and control logic are separated. It is widely used in web frameworks such as Laravel, Django, Spring MVC, ASP.NET MVC and Ruby on Rails.

Components

  • Model: Represents data and business rules. It reads and writes the database, validates data and notifies the view of changes. It does not know about the UI.
  • View: The presentation layer; displays model data to the user (HTML templates, JSP, Blade). It contains little or no logic.
  • Controller: Receives user input (HTTP requests), calls the model to get or update data, and chooses which view to show. It acts as the mediator.
          +-------------+  1. request
 User --->| Controller  |<------------- Browser
          +--+-------+--+
   2. update/ |       | 4. select view
      query   v       v
          +-------+  3. data  +-------+
          | Model |---------->| View  |
          +-------+           +---+---+
                                  | 5. HTML response
                                  v  User

Flow of a web request

  1. The browser requests a URL; routing sends it to a controller action.
  2. The controller asks the model for data (e.g. a list of products).
  3. The model queries the database and returns the data.
  4. The controller passes the data to a view.
  5. The view renders HTML, which is sent back to the user.

Advantages

  • Separation of concerns; easier maintenance and testing.
  • Multiple views can use the same model (web, mobile).
  • Developers and designers can work in parallel.
  • Code reuse and easier extension.

Disadvantage: extra structure makes small applications more complex.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Web Technology chapter 5 questions →