×

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a data model, display information, and control information. All these elements must be divided into separate objects to follow the MVC design.

Model-View-Controller Pattern is also known as the MVC pattern. This pattern is employed to divide the concerns of an application.

• Model: Model depicts a data-carrying object or Java POJO. Additionally, data changes may include logic to update the Controller.

• View: View represents how the model's data is visualized.

• Controller: The Controller affects both the View and the model. It refreshes the display and manages the data flow into the model object whenever data changes. View and model are kept apart.

The Model in Java programming comprises the basic Java classes, the Controller houses the servlets, and the View is used to show the data.

As a result of this division, the user requests are handled as follows:

1. The Controller on the server side receives a request for a page from the client.

2. The model is then called by the Controller. It compiles the needed information.

3. The data is sent from the Controller to the view layer.

4. The View is now returning the result to the client, which is the browser.

Benefits of the MVC Architecture

1. Scalability is a property of MVC that aids in expanding applications.

2. There is minimal dependence, making maintenance of the components simple.

3. Multiple views can reuse a model, allowing code reuse.

4. The developers can all work on the three layers—Model, View, and Controller—at once.

5. The program is made easier to comprehend by using MVC.

6. Each layer is maintained individually using MVC, so we don't need to deal with a tone of code.

7. It is simpler to test and extend applications.

Java implementation of MVC

We'll make an Employee object that will serve as a model. EmployeeView will be a view class that may publish Employee information on the console. EmployeeController is the controller class in charge of storing data in the Employee object and updating the View EmployeeView as necessary.

MVC Program in Java

EM.java

public class EM { 
// Declaring the variables
private String employee_name; 
private String employee_id; 
private String employee_department; 
// getId() method is used to get the id
public String getId() { 
return employee_id; 
} 
// setId() method is used to set the id 
public void setId(String id) { 
this.employee_id = id; 
} 
// getName() method is used to get the name
public String getName() { 
return employee_name; 
} 
// setName() method is used to set the name
public void setName(String name) { 
this.employee_name = name; 
} 
// get department() method is used to get the Department 
public String getDepartment() { 
return employee_department; 
} 
/ setDepartment() method is used to set the Department 
public void setDepartment(String Department) { 
this.employee_department = Department; 
} 
} 

EV.java

public class EV { 
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); 
} 
} 

EC.java

public class EC { 
private Em model; 
private EV v1; 
public EC(Em model, EV v1) { 
this.model = model; 
this.v1 = v1; 
} 
public void setEmployee_name(String name){ 
model.setName(name); 
} 
public String getEmployee_name(){ 
return model.getName(); 
} 
public void setEmployee_id(String id){ 
model.setId(id); 
} 
public String getEmployee_id(){ 
return model.getId(); 
} 
public void setEmployee_department(String Department){ 
model.setDepartment(Department); 
} 
public String getEmployee_department(){ 
return model.getDepartment(); 
} 
public void updateView() { 
v1.printEmployeeDetails(model.getName(), model.getId(), model.getDepartment()); 
} 
}

MVC1.java

public class MVC1{ 
public static void main(String[] args) { 
// retrieving the employee record from the database using the employee id 
Em m1 = retriveEmployeeFromDatabase(); 
// making a view to display employee information on the console 
EV v1 = new EV(); 
EC c1 = new EC(m1, v1); 
c1.updateView(); 
c1.setEmployee_name("kamal"); 
System.out.println("\n Details of employee after updation: "); 
c1.updateView(); 
} 
private static Em retriveEmployeeFromDatabase(){ 
Em E1 = new Em(); 
E1.setName("Rohith"); 
E1.setId("30"); 
E1.setDepartment("manager"); 
return E1; 
} 
} 

Output

MVC in Java

Program2 For MVC in Java

Student1.java

public class Student1 {
private String roll_no;
private String name;
public String getRollNo() {
return roll_no;
}
public void setRollNo(String roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

SV.java

public class SV {
public void printStudentDetails(String studentName, String studentRollNo){
System.out.println("Student: ");
System.out.println("Name: " + studentName);
System.out.println("Roll No: " + studentRollNo);
}
}

SC.java

public class SC {
private Student1 model;
private SV v1;
public SC(Student1 model, SV v1){
this.model = model;
this.v1 = v1;
}
public void setStudent_name(String name){
model.setName(name); 
}
public String getStudent_name(){
return model.getName(); 
}
public void setStudent_rollNo(String rollNo){
model.setRollNo(rollNo); 
}
public String getStudent_rollNo(){
return model.getRollNo(); 
}
public void updateView(){ 
v1.printStudentDetails(model.getName(), model.getRollNo());
} 
}

MVC2.java

public class MVC2 {
public static void main(String[] args) {
Student1 m1 = retriveStudentFromDatabase();
SV v1 = new SV();
SC c1 = new SC(m1, v1);
c1.updateView();
c1.setStudent_name("Rohith");
c1.updateView();
}
private static Student1 retriveStudentFromDatabase(){
Student1 s1 = new Student1();
s1.setName("kamal");
s1.setRollNo("10");
return s1;
}
}

Output

MVC in Java

Related Topics

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.