MVC Design Pattern

Topics Covered

Overview

MVC is an architectural/design pattern that isolates the application's logic into three parts: Model, View, and Controller. The isolation is performed to assign specific responsibilities for each part. The Model represents the data, Controller handles the business logic, and View handles the presentation logic.

When Will We Need MVC Design Pattern?

MVC design pattern is used commonly while developing web applications. When we open a web page, the request goes to the server, which processes the request, reads data from the database, and renders the data on the web page. This most common web application use case fits nicely into the MVC design pattern.

When the request reaches the server, Controller takes care of handling the request, then reads the database and maps the data to the Model, and renders the retrieved data in the View

How Does MVC Design Pattern Work?

Let's understand the working MVC design pattern using a web application use case. Even though we won't be writing any web-related code, we can understand the crux of it using a Java example.

mvc design pattern working

MVC design pattern is based on three components: Model, View, and Controller. The controller is the entry point in the MVC pattern. When the controller handles the request, it interacts with the datastore to fetch data and maps it to the Model; then, the model is passed to the View, which renders it.

Pseudocode

In this example, we create three classes User, UserController, and UserView. The User class refers to the user model. The UserController class refers to the controller that interacts with the data store and maps the response to the model and pass it to view. The UserView class refers to the view for the user where we print user information read from the model.

pseudocode mvc design

User

UserDataStore

UserController

UserView

Main

Language Specific Code (Java + C++ + Python)

Java

User

UserDataStore

UserController

UserView

Main

Output

C ++

User

UserDataStore

UserController

UserView

Main

Output

Python User

UserDataStore

UserController

UserView

Main

Output

Pros and Cons of MVC Design Pattern

Pros of MVC Design Pattern

  • The business logic and presentation layer is isolated.
  • Development is faster for bigger projects. As the business logic and presentation layer is separated, multiple groups can work parallelly on business logic and presentation logic. For smaller projects, we can do quick prototyping without using MVC boilerplate

Cons of MVC Design Pattern

  • Not the best fit for small web applications and will be an overburden.

Relations with Other Patterns

The MVVM (Model-View-ViewModel) design pattern is similar to the MVC design pattern. Here ViewModel is responsible for rendering the view from the Model and provides its own methods and internal state to update the view.

Conclusion

  • MVC is an architectural/design pattern that isolates the application's logic into three parts: Model, View, and Controller.
  • In the MVC pattern, the Model represents the data, Controller handles the business logic, and View handles the presentation logic.
  • MVC design pattern is used commonly while developing web applications.