This project is a simple demonstration of the Model-View-Controller (MVC) design pattern implemented in Java. It showcases how to separate the application's concerns into three interconnected components.
- Model (
Student.java): Represents the data and the business logic. In this case, it's a simpleStudentobject with a name and a roll number. - View (
StudentView.java): The user interface of the application. It displays the data from the model to the user. Here, it's a console output that prints the student's details. - Controller (
StudentController.java): Acts as an intermediary between the Model and the View. It handles user input, updates the model, and tells the view to refresh its display.
- Java Development Kit (JDK) 8 or higher
- Apache Maven
-
Compile the project: Open a terminal in the project root directory and run the following Maven command to compile the source code.
mvn compile
-
Run the application: Execute the main class to see the MVC pattern in action.
mvn exec:java -Dexec.mainClass="JeyZ9.MVCPatternDemo"
You should see the following output in your console:
Student:
Name: John
Roll No: 10
Student:
Name: Somchai
Roll No: 12
This diagram shows the static structure and relationships between the classes.
Click to view Class Diagram
classDiagram
class MVCPatternDemo {
+main(String[] args)
}
class StudentController {
-model: Student
-view: StudentView
+StudentController(Student model, StudentView view)
+setStudentName(String name)
+setStudentRollNo(String rollNo)
+updateView()
}
class Student {
-name: String
-rollNo: String
+getName(): String
+setName(String name)
+getRollNo(): String
+setRollNo(String rollNo)
}
class StudentView {
+printStudentDetails(String studentName, String studentRollNo)
}
MVCPatternDemo ..> StudentController : Creates
MVCPatternDemo ..> Student : Creates
MVCPatternDemo ..> StudentView : Creates
StudentController o-- Student : model
StudentController o-- StudentView : view
This diagram illustrates the sequence of interactions between the objects during runtime.
Click to view Sequence Diagram
sequenceDiagram
participant Client as MVCPatternDemo
participant Controller as StudentController
participant Model as Student
participant View as StudentView
Client->>Model: new Student()
Client->>Model: setName("John")
Client->>Model: setRollNo("10")
Client->>View: new StudentView()
Client->>Controller: new StudentController(model, view)
Client->>Controller: updateView()
Controller->>Model: getName()
Model-->>Controller: "John"
Controller->>Model: getRollNo()
Model-->>Controller: "10"
Controller->>View: printStudentDetails("John", "10")
Client->>Controller: setStudentName("Somchai")
Controller->>Model: setName("Somchai")
Client->>Controller: setStudentRollNo("12")
Controller->>Model: setRollNo("12")
Client->>Controller: updateView()
Controller->>Model: getName()
Model-->>Controller: "Somchai"
Controller->>Model: getRollNo()
Model-->>Controller: "12"
Controller->>View: printStudentDetails("Somchai", "12")