MVC Pattern

MVC pattern stands for Model-View-Controller pattern. This pattern is used for layered development of applications.

  • ModelThe model represents an object that accesses data or a JAVA POJO. It can also contain logic to update the controller when data changes.
  • View- View represents the visualization of the data contained in the model.
  • ControllerThe controller acts on the model and the view. It controls the flow of data to model objects and updates the view when data changes. It separates the view from the model.


summary

intent

Divides the application into three core components: Model, View, and Controller, to achieve separation of concerns.

Main problems solved

  • It solves the coupling problem among business logic, data, and interface display in applications, making development and maintenance clearer and simpler.

Use Case

  • When data, business logic, and interface display need to be separated for independent development and maintenance.

Implementation Approach

  • Model: Responsible for data and business logic, usually including data storage, retrieval, and business rules.
  • View: Responsible for displaying the user interface of the data (model), without business logic.
  • Controller: Receives user input, calls the model and view to complete the user's request.

Key code

  • Model: Contains business logic and data state.
  • view: Contains presentation logic, renders the model's data as a user interface.
  • Control unit: Contains logic to respond to user input and update the model and view.

Application example

  • Web application: The user sends a request through the 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 to reduce coupling.
  • Easy to maintain: Each component is responsible for a specific task, making it easier to develop and maintain independently.
  • Extensibility: Can independently replace or update the model, view, or controller.

Disadvantages

  • May increase complexity: For simple projects, introducing MVC may add unnecessary complexity.
  • Performance issues: If not used correctly, it may cause performance issues.

Usage suggestions

  • When developing large applications that require clear separation of data, business logic, and user interface, consider using the MVC pattern.

Notes

  • Ensure clear interaction among the model, view, and controller, avoiding mutual dependencies.

Several main roles included

  • Model:

    • Manages data and business logic.
  • View:

    • Displays data in the model and provides a user interface.
  • Controller:

    • Receives user input, calls the model and view for processing.
  • User (optional):

    • Interacts with the view and triggers the application flow.

Implementation

We will create a ... as the modelStudentObject.StudentViewIs a view class that outputs student details to the console,StudentControlleris responsible for storing data toStudentthe controller class in the object, and updates the view accordingly.StudentView。

MVCPatternDemo, our demo class usesStudentControllerto demonstrate the usage of the MVC pattern.

MVC 模式的 UML 图

Step 1

Create 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 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 a 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

UsageStudentControllermethod to demonstrate the usage of the MVC design pattern.

MVCPatternDemo.java

public class MVCPatternDemo { public static void main(String[] args) { //Fetch student records from the database Student model = retrieveStudentFromDatabase(); //Create a view: output student details to the console StudentView view = new StudentView(); StudentController controller = new StudentController(model, view); controller.updateView(); //Update model data controller.setStudentName("John"); controller.updateView(); } private static Student retrieveStudentFromDatabase(){ Student student = new Student(); student.setName("Robert"); student.setRollNo("10"); return student; } }

Step 5

Run the program, output the results:

Student: 
Name: Robert
Roll No: 10
Student: 
Name: John
Roll No: 10
other extensions