Mvc Pattern
The MVC pattern stands for Model-View-Controller pattern. This pattern is used for separating an application into three main logical components: the model, the view, and the controller.
* **Model** - The model represents an object carrying data. It can also contain logic to update the controller if its data changes.
* **View** - The view represents the visualization of the data that model contains.
* **Controller** - The controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate.
!(#)
* * *
## Summary
### Intent
Separate an application's concerns by dividing it into three core components: Model, View, and Controller.
### Problem It Solves
* It solves the problem of coupling business logic, data, and interface display within an application, making development and maintenance clearer and simpler.
### When to Use
* When there is a need to separate data, business logic, and interface display for independent development and maintenance.
### Implementation
* **Model**: Responsible for data and business logic, typically including data storage, retrieval, and business rules.
* **View**: Responsible for the user interface that displays the data (model), containing no business logic.
* **Controller**: Receives user input, invokes the model and view to fulfill the user's request.
### Key Code
* **Model**: Contains business logic and data state.
* **View**: Contains presentation logic, rendering the model's data into a user interface.
* **Controller**: Contains logic to respond to user input and update the model and view.
### Application Example
* **Web Application**: A user sends a request through a browser (view), the server-side controller processes the request, and the model performs data processing.
### Advantages
* **Separation of Concerns**: Separates data, business logic, and interface display, reducing coupling.
* **Easy Maintenance**: Each component is responsible for a specific task, facilitating independent development and maintenance.
* **Scalability**: The model, view, or controller can be replaced or updated independently.
### Disadvantages
* **May Increase Complexity**: For simple projects, introducing MVC might add unnecessary complexity.
* **Performance Issues**: If not used correctly, it can lead to performance problems.
### Usage Suggestions
* Consider using the MVC pattern when developing large applications that require a clear separation of data, business logic, and user interface.
### Notes
* Ensure the interactions between the model, view, and controller are clear to avoid interdependencies.
### Main Roles Involved
* **Model**:
* Manages data and business logic.
* **View**:
* Displays the data from the model, providing the user interface.
* **Controller**:
* Receives user input, invokes the model and view for processing.
* **User (Optional)**:
* Interacts with the view, triggering the application's flow.
* * *
## Implementation
We will create a _Student_ object as the model. _StudentView_ is a view class that prints student details to the console, and _StudentController_ is a controller class responsible for storing data in the _Student_ object and updating the view _StudentView_ accordingly.
_MVCPatternDemo_, our demo class, uses _StudentController_ to demonstrate the usage of the MVC pattern.

## Step 1
Create the model.
## Student.java
public 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; }}
## Step 2
Create the view.
## StudentView.java
public class StudentView{public void printStudentDetails(String studentName, String studentRollNo){System.out.println("Student: "); System.out.println("Name: " + studentName); System.out.println("Roll No: " + studentRollNo); }}
## Step 3
Create the controller.
## StudentController.java
public 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()); }}
## Step 4
Use _StudentController_ methods to demonstrate the usage of the MVC design pattern.
## MVCPatternDemo.java
public class MVCPatternDemo{public static void main(String[]args){Student model = retrieveStudentFromDatabase(); StudentView view = new StudentView(); StudentController controller = new StudentController(model, view); controller.updateView(); controller.setStudentName("John"); controller.updateView(); }private static Student retrieveStudentFromDatabase(){Student student = new Student(); student.setName("Robert"); student.setRollNo("10"); return student; }}
## Step 5
Execute the program, and the output will be:
Student: Name: RobertRoll No: 10Student: Name: JohnRoll No: 10
YouTip