Open In App

MVC Design Pattern

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.



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.



Components of the MVC Design Pattern

1. Model

The Model component in the MVC (Model-View-Controller) design pattern represents 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

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:

1. Model (Student class)

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




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.




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).




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:




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;
    }
}




Student:
Name: Lokesh Sharma
Roll No: 15UCS157
Student:
Name: Vikram Sharma
Roll No: 15UCS157

Advantages of the MVC Design Pattern

Disadvantages of the MVC Design Pattern


Article Tags :