Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
Observer Design Pattern
Next article icon

MVC Design Pattern

Last Updated : 03 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The MVC design pattern is a software architecture pattern that separates an application into three main components: Model, View, and Controller, making it easier to manage and maintain the codebase. It also allows for the reusability of components and promotes a more modular approach to software development.

MVC-design-pattern

Table of Content

  • What is the MVC Design Pattern?
  • Components of the MVC Design Pattern
  • Communication between the Components
  • Example of the MVC Design Pattern
  • Advantages of the MVC Design Pattern
  • Disadvantages of the MVC Design Pattern

What is the MVC Design Pattern?

The Model View Controller (MVC) design pattern specifies that an application consists of a data model, presentation information, and control information. The pattern requires that each of these be separated into different objects.

  • The MVC pattern separates the concerns of an application into three distinct components, each responsible for a specific aspect of the application’s functionality.
  • This separation of concerns makes the application easier to maintain and extend, as changes to one component do not require changes to the other components. 

Why use MVC Design Pattern?

The MVC (Model-View-Controller) design pattern breaks an application into three parts: the Model (which handles data), the View (which is what users see), and the Controller (which connects the two). This makes it easier to work on each part separately, so you can update or fix things without messing up the whole app. It helps developers add new features smoothly, makes testing simpler, and allows for better user interfaces. Overall, MVC helps keep everything organized and improves the quality of the software.

Components of the MVC Design Pattern

MVC-Design-Pattern-

1. Model

The Model component in the MVC (Model-View-Controller) design pattern demonstrates the data and business logic of an application. It is responsible for managing the application’s data, processing business rules, and responding to requests for information from other components, such as the View and the Controller.

2. View

Displays the data from the Model to the user and sends user inputs to the Controller. It is passive and does not directly interact with the Model. Instead, it receives data from the Model and sends user inputs to the Controller for processing.

3. Controller

Controller acts as an intermediary between the Model and the View. It handles user input and updates the Model accordingly and updates the View to reflect changes in the Model. It contains application logic, such as input validation and data transformation.

Communication between the Components

This below communication flow ensures that each component is responsible for a specific aspect of the application’s functionality, leading to a more maintainable and scalable architecture

  • User Interaction with View: The user interacts with the View, such as clicking a button or entering text into a form.
  • View Receives User Input: The View receives the user input and forwards it to the Controller.
  • Controller Processes User Input: The Controller receives the user input from the View. It interprets the input, performs any necessary operations (such as updating the Model), and decides how to respond.
  • Controller Updates Model: The Controller updates the Model based on the user input or application logic.
  • Model Notifies View of Changes: If the Model changes, it notifies the View.
  • View Requests Data from Model: The View requests data from the Model to update its display.
  • Controller Updates View: The Controller updates the View based on the changes in the Model or in response to user input.
  • View Renders Updated UI: The View renders the updated UI based on the changes made by the Controller.

Example of the MVC Design Pattern

Below is the code of above problem statement using MVC Design Pattern:

Let’s break down into the component wise code:

Untitled-Diagram-(1)

1. Model (Student class)

Represents the data (student’s name and roll number) and provides methods to access and modify this data.

Java
class Student {     private String rollNo;     private String name;      public String getRollNo() {         return rollNo;     }      public void setRollNo(String rollNo) {         this.rollNo = rollNo;     }      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     } } 

2. View (StudentView class)

Represents how the data (student details) should be displayed to the user. Contains a method (printStudentDetails) to print the student’s name and roll number.

Java
class StudentView {     public void printStudentDetails(String studentName, String studentRollNo) {         System.out.println("Student:");         System.out.println("Name: " + studentName);         System.out.println("Roll No: " + studentRollNo);     } } 

3. Controller (StudentController class)

Acts as an intermediary between the Model and the View. Contains references to the Model and View objects. Provides methods to update the Model (e.g., setStudentName, setStudentRollNo) and to update the View (updateView).

Java
class StudentController {     private Student model;     private StudentView view;      public StudentController(Student model, StudentView view) {         this.model = model;         this.view = view;     }      public void setStudentName(String name) {         model.setName(name);     }      public String getStudentName() {         return model.getName();     }      public void setStudentRollNo(String rollNo) {         model.setRollNo(rollNo);     }      public String getStudentRollNo() {         return model.getRollNo();     }      public void updateView() {         view.printStudentDetails(model.getName(), model.getRollNo());     } } 

Complete code for the above example

Below is the complete code for the above example:

Java
class Student {     private String rollNo;     private String name;      public String getRollNo() {         return rollNo;     }      public void setRollNo(String rollNo) {         this.rollNo = rollNo;     }      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     } }  class StudentView {     public void printStudentDetails(String studentName, String studentRollNo) {         System.out.println("Student:");         System.out.println("Name: " + studentName);         System.out.println("Roll No: " + studentRollNo);     } }  class StudentController {     private Student model;     private StudentView view;      public StudentController(Student model, StudentView view) {         this.model = model;         this.view = view;     }      public void setStudentName(String name) {         model.setName(name);     }      public String getStudentName() {         return model.getName();     }      public void setStudentRollNo(String rollNo) {         model.setRollNo(rollNo);     }      public String getStudentRollNo() {         return model.getRollNo();     }      public void updateView() {         view.printStudentDetails(model.getName(), model.getRollNo());     } }  public class MVCPattern {     public static void main(String[] args) {         Student model = retriveStudentFromDatabase();          StudentView view = new StudentView();          StudentController controller = new StudentController(model, view);          controller.updateView();          controller.setStudentName("Vikram Sharma");          controller.updateView();     }      private static Student retriveStudentFromDatabase() {         Student student = new Student();         student.setName("Lokesh Sharma");         student.setRollNo("15UCS157");         return student;     } } 
Output
Student: Name: Lokesh Sharma Roll No: 15UCS157 Student: Name: Vikram Sharma Roll No: 15UCS157 

When to Use the MVC Design Pattern

Below is when to use MVC Design Pattern:

  • Complex Applications: Use MVC for apps with many features and user interactions, like e-commerce sites. It helps organize code and manage complexity.
  • Frequent UI Changes: If the UI needs regular updates, MVC allows changes to the View without affecting the underlying logic.
  • Reusability of Components: If you want to reuse parts of your app in other projects, MVC’s modular structure makes this easier.
  • Testing Requirements: MVC supports thorough testing, allowing you to test each component separately for better quality control.

When Not to Use the MVC Design Pattern

Below is when not to use MVC Design Pattern:

  • Simple Applications: For small apps with limited functionality, MVC can add unnecessary complexity. A simpler approach may be better.
  • Real-Time Applications: MVC may not work well for apps that require immediate updates, like online games or chat apps.
  • Tightly Coupled UI and Logic: If the UI and business logic are closely linked, MVC might complicate things further.
  • Limited Resources: For small teams or those unfamiliar with MVC, simpler designs can lead to faster development and fewer issues.


Next Article
Observer Design Pattern
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Design Pattern
  • System Design

Similar Reads

  • Design Patterns Gamma
    Elements of Reusable Object-Oriented Software is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword by Grady Booch. The book is divided into two parts, with the first two chapters expl
    6 min read
  • Memento Design Pattern
    The Memento Design Pattern is a behavioral pattern that helps save and restore an object's state without exposing its internal details. It is like a "snapshot" that allows you to roll back changes if something goes wrong. It is widely used in undo-redo functionality in applications like text editors
    6 min read
  • Observer Design Pattern
    The Observer Design Pattern is a behavioral design pattern that defines a one-to-many dependency between objects. When one object (the subject) changes state, all its dependents (observers) are notified and updated automatically. Table of Content What is the Observer Design Pattern?Real-world analog
    8 min read
  • Mediator Design Pattern
    The Mediator Design Pattern simplifies communication between multiple objects in a system by centralizing their interactions through a mediator. Instead of objects interacting directly, they communicate via a mediator, reducing dependencies and making the system easier to manage. Table of Content Wh
    7 min read
  • Microservices Design Patterns
    Microservices Design Patterns explains how to build and manage microservices, which are small, independent services that work together in an application. It introduces different design patterns, or best practices, that help in organizing these services effectively. These patterns cover various aspec
    11 min read
  • SAGA Design Pattern
    The SAGA Design Pattern is a pattern used to manage long-running and distributed transactions, particularly in microservices architecture. Unlike traditional monolithic transactions, which require a single, centralized transaction management system, the SAGA pattern breaks down a complex transaction
    8 min read
  • State Design Pattern
    The State design pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. It achieves this by encapsulating the object's behavior within different state objects, and the object itself dynamically switches between these state objects
    11 min read
  • Proxy Design Pattern
    The Proxy Design Pattern a structural design pattern is a way to use a placeholder object to control access to another object. Instead of interacting directly with the main object, the client talks to the proxy, which then manages the interaction. This is useful for things like controlling access, d
    9 min read
  • Null Object Design Pattern
    The Null Object Design Pattern is a behavioral design pattern that is used to provide a consistent way of handling null or non-existing objects. It is particularly useful in situations where you want to avoid explicit null checks and provide a default behavior for objects that may not exist. Importa
    7 min read
  • Object Pool Design Pattern
    The Object Pool Design Pattern is a creational design pattern that manages a pool of reusable objects to minimize the overhead of creating and destroying objects. It maintains a collection of initialized objects and provides mechanisms for clients to efficiently borrow and return objects from the po
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences