Latest web development tutorials

MVC pattern

MVC model represents the Model-View-Controller (Model - View - Controller) model. This mode is used for layered application development.

  • Model (Model) - model represents a data access objects or JAVA POJO.It may also carry logic, update controller when data changes.
  • View (View) - view model contains representatives of visualization.
  • Controller (Controller) - Controller role model and the view on to.It controls the data flow model object, and update the view when data changes. It makes the view and model separated.

achieve

We will create aStudentobject model as.StudentViewis a student of the output to the console details viewclass,StudentController is responsible for storing the data to theStudentobject controller classes, and updates the viewStudentView.

MVCPatternDemo,we demonstrate the use ofStudentControllerclass to demonstrate the use of the MVC pattern.

MVC pattern UML diagram

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

UseStudentControllermethod to demonstrate the use of the MVC design pattern.

MVCPatternDemo.java

public class MVCPatternDemo {
   public static void main (String [] args) {

      // Can obtain student records Student model = retriveStudentFromDatabase () from the data;

      // Create a view: the output to the console student details StudentView view = new StudentView ();

      StudentController controller = new StudentController (model, view);

      controller.updateView ();

      // Update model data controller.setStudentName ( "John");

      controller.updateView ();
   }

   private static Student retriveStudentFromDatabase () {
      Student student = new Student ();
      student.setName ( "Robert");
      student.setRollNo ( "10");
      return student;
   }
}

Step 5

Verify output.

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