# Model View Controller (MVC)

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Model View Controller (MVC) (which is a Architecture Pattern).

# Model View Controller (MVC) Pattern - theory

Model View Controller (MVC) Pattern in Java divides a given application into three interconnected parts (model, view and controller) in order to separate internal representations of information from the ways that information is presented to and accepted from the user. This pattern is used in frameworks and for this reason we speak about MVC frameworks. In MVC frameworks (like Spring, for instance) the user (or a Java client) is not able to connect to the data sources himself, but he has to interact with the top-level layers to access the data. The user interact with the view and with the controller, but not with the model.

In a Model View Controller (MVC) Pattern,

  • the Model: represents data and the rules that govern access to and updates of this data (it can also have business logic).

  • the View: displays the data and also takes input from user.

  • the Controller: acts as an interface between the Model and the View parts, and it intercepts all the incoming requests.

This kind of software architecture pattern helps the parallel development and is used to separate application's concerns. Take a look at the following UML diagram representing the Model View Controller (MVC) pattern (for my example):

# Model View Controller (MVC) Pattern - example

Here is an example of using the Model View Controller (MVC) Design Pattern in Java:

package modelviewcontroller.java.pattern.example;
 
public class EmployeeModel {
    
   private int id;
   private String name;
   private String job;
 
   public EmployeeModel(int id, String name, String job) {
       super();
       this.id = id;
       this.name = name;
       this.job = job;
    }
 
   public int getId() {
       return id;
   }
 
   public String getName() {
       return name;
   }
    
   public String getJob() {
       return job;
   }
 
   public void setId(int id) {
       this.id = id;
   }
 
   public void setName(String name) {
       this.name = name;
   }
 
   public void setJob(String job) {
     this.job = job;
   }
}
package modelviewcontroller.java.pattern.example;
 
public class EmployeeView {
 
    public void printEmployeeInformation(EmployeeModel emp){
          System.out.println("Employee [ID="+emp.getId()+" /Name="+emp.getName()+" /Job="+emp.getJob()+"]");
       }
}
package modelviewcontroller.java.pattern.example;
 
public class Controller {
 
    private EmployeeModel employeeModel;
    private EmployeeView employeeView;
     
    public Controller(EmployeeModel employeeModel, EmployeeView employeeView) {
        super();
        this.employeeModel = employeeModel;
        this.employeeView = employeeView;
    }
     
    public void setEmployeeId (int id) {
        employeeModel.setId(id); 
        updateView();
    }
     
    public int getEmployeeId () {
        return employeeModel.getId(); 
    }
     
    public void setEmployeeName (String name) {
        employeeModel.setName(name); 
        updateView();
    }
     
    public String getEmployeeName () {
        return employeeModel.getName(); 
    }
     
    public void setEmployeeJob (String job) {
        employeeModel.setJob(job); 
        updateView();
    }
     
    public String getEmployeeJob() {
        return employeeModel.getJob(); 
    }
     
    public void updateView(){               
        employeeView.printEmployeeInformation(employeeModel);
    }
}
package modelviewcontroller.java.pattern.example;
 
public class MvcJavaPatternExample {
 
    public static void main(String[] args) {
         
        EmployeeModel employeeModel = new EmployeeModel(10, "John Smith", "Analyst");
        EmployeeView employeeView = new EmployeeView();
                 
        Controller controller = new Controller(employeeModel, employeeView);
         
        controller.updateView();
         
        System.out.println("\nVIEW UPDATES generated automatically by the controller: ");
        System.out.println("------------------------------------------------------- ");
         
        controller.setEmployeeJob("Programmer");
        controller.setEmployeeJob("Manager");
        controller.setEmployeeName("Dan Travolta");
        controller.setEmployeeId(200);
         
    }
}

When you run this example you will receive the following result: