Design Patterns - MVC Pattern
The Model-View-Controller (MVC) is a well-known design pattern in the web development field. It is way
to organize our code. It specifies that a program or application shall consist of data model, presentation
information and control information. The MVC pattern needs all these components to be separated as
different objects.
What is MVC architecture in Java?
The model designs based on the MVC architecture follow MVC design pattern. The application logic is
separated from the user interface while designing the software using model designs.
The MVC pattern architecture consists of three layers:
Model: It represents the business layer of application. It is an object to carry the data that can also
contain the logic to update controller if data is changed.
View: It represents the presentation layer of application. It is used to visualize the data that the model
contains.
Controller: It works on both the model and view. It is used to manage the flow of application, i.e. data
flow in the model object and to update the view whenever data is changed.
In Java Programming, the Model contains the simple Java classes, the View used to display the data and
the Controller contains the servlets. Due to this separation the user requests are processed as follows:
1. A client (browser) sends a request to the controller on the server side, for a page.
2. The controller then calls the model. It gathers the requested data.
3. Then the controller transfers the data retrieved to the view layer.
4. Now the result is sent back to the browser (client) by the view.
Implementation of MVC using Java
To implement MVC pattern in Java, we are required to create the following three classes.
Employee Class, will act as model layer
EmployeeView Class, will act as a view layer
EmployeeContoller Class, will act a controller layer
Employee.java
public class Employee {
// declaring the variables
private String EmployeeName;
private String EmployeeId;
private String EmployeeDepartment;
// defining getter and setter methods
public String getId() {
return EmployeeId;
public void setId(String id)
this.EmployeeId = id;
public String getName() {
return EmployeeName;
}
public void setName(String name) {
this.EmployeeName = name;
public String getDepartment() {
return EmployeeDepartment;
public void setDepartment(String Department) {
this.EmployeeDepartment = Department;
EmployeeView.java
public class EmployeeView
// method to display the Employee details
public void printEmployeeDetails (String EmployeeName, String EmployeeId, String
EmployeeDepartment)
{ System.out.println("Employee Details: ");
System.out.println("Name: " + EmployeeName);
System.out.println("Employee ID: " + EmployeeId);
System.out.println("Employee Department: " + EmployeeDepartment);
}
EmployeeController.java
// class which represent the controller
public class EmployeeController {
// declaring the variables model and view
private Employee model;
private EmployeeView view;
// constructor to initialize
public EmployeeController(Employee model, EmployeeView view) {
this.model = model;
this.view = view;
// getter and setter methods
public void setEmployeeName(String name){
model.setName(name);
public String getEmployeeName(){
return model.getName();
public void setEmployeeId(String id){
model.setId(id);
public String getEmployeeId(){
return model.getId(); }
public void setEmployeeDepartment(String Department){
model.setDepartment(Department);
public String getEmployeeDepartment(){
return model.getDepartment();
// method to update view
public void updateView() {
view.printEmployeeDetails(model.getName(), model.getId(), model.getDepartment());
MVCMain.java
public class MVCMain {
public static void main(String[] args) {
// fetching the employee record based on the employee_id from the database
Employee model = retriveEmployeeFromDatabase();
// creating a view to write Employee details on console
EmployeeView view = new EmployeeView();
EmployeeController controller = new EmployeeController(model, view);
controller.updateView();
//updating the model data
controller.setEmployeeName("Nirnay");
System.out.println("\n Employee Details after updating: ");
controller.updateView();
private static Employee retriveEmployeeFromDatabase(){
Employee Employee = new Employee();
Employee.setName("Anu");
Employee.setId("11");
Employee.setDepartment("Salesforce");
return Employee;