0% found this document useful (0 votes)
293 views13 pages

Diff Between MVT & MVC

The document discusses the MVC and MVT design patterns. MVC separates an application into three layers - the model, view, and controller. The model manages the data logic, the view displays the presentation, and the controller handles input and output between the model and view. MVT is similar but replaces the controller with templates. Templates handle the user interface and presentation logic. The document provides examples of implementing MVC and MVT in Java applications and compares the advantages and differences between the two patterns.

Uploaded by

Soham Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
293 views13 pages

Diff Between MVT & MVC

The document discusses the MVC and MVT design patterns. MVC separates an application into three layers - the model, view, and controller. The model manages the data logic, the view displays the presentation, and the controller handles input and output between the model and view. MVT is similar but replaces the controller with templates. Templates handle the user interface and presentation logic. The document provides examples of implementing MVC and MVT in Java applications and compares the advantages and differences between the two patterns.

Uploaded by

Soham Datta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

3) MVC and MVT concept

Mvc(Model-View-Controller Pattern)

MVC Pattern stands for Model-View-Controller Pattern.


This pattern is used to separate application's concerns. Model
- Model represents an object or JAVA POJO carrying data. It
can also have logic to update controller if its data changes.
View - View represents the visualization of the data that
model contains.
MVC Architecture in Java:

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:

o 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.
o View: It represents the presentation layer of
application. It is used to visualize the data that the
model contains.
o 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
Implementation

We are going to create a Student object acting as a


model.StudentView will be a view class which can print
student details on console and StudentController is the
controller class responsible to store data
in Student object and update
view StudentView accordingly.
MVCPatternDemo,our demo class, will
use StudentController to demonstrate use of MVC
pattern.
Step 1
Create 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 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
Use the StudentController methods to demonstrate MVC
design pattern usage.
MVCPatternDemo.java
public class MVCPatternDemo {
public static void main(String[] args) {

//fetch student record based on his roll no from the


database
Student model = retriveStudentFromDatabase();

//Create a view : to write student details on console


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 the output.

Student:
Name: Robert
Roll No: 10
Student:
Name: John
Roll No: 10
Advantages of MVC Architecture
The advantages of MVC architecture are as follows:

o MVC has the feature of scalability that in turn


helps the growth of application.
o The components are easy to maintain because there
is less dependency.
o A model can be reused by multiple views that
provides reusability of code.
o The developers can work with the three layers
(Model, View, and Controller) simultaneously.
o Using MVC, the application becomes more
understandable.
o Using MVC, each layer is maintained separately
therefore we do not require to deal with massive
code.
o The extending and testing of application is easier.
MVT(Model View Template )
The MVT (Model View Template) is a software
design pattern. It is a collection of three important
components Model View and Template. The Model
helps to handle database. It is a data access layer which
handles the data. The Template is a presentation layer
which handles User Interface part completely.
MVT Structure

o Model: This is an abstraction layer for


structuring and manipulating the data of the
Web Application. It acts as an interface for
maintaining data. This is a logical data
structure behind the entire application and
helps to handle the database.
o View: This layer encapsulates the logic
responsible for processing a user’s request and
returns a response. It is a user interface to
execute the logic and interact with the models.
It is responsible for displaying all or a portion
of data to the user.
o Template: The template layer provides a
designer-friendly syntax for rendering the
information to be presented to the user. It
contains the static parts of the desired HTML
output along with some special syntax, also
known as Django Template Language (DTL),
describing how dynamic content will be
inserted.

Django MVT

The MVT (Model View Template) is a software design


pattern. It is a collection of three important components
Model View and Template. The Model helps to handle
database. It is a data access layer which handles the
data.
The Template is a presentation layer which handles
User Interface part completely. The View is used to
execute the business logic and interact with a model to
carry data and renders a template.
Although Django follows MVC pattern but maintains
it’s own conventions. So, control is handled by the
framework itself.
There is no separate controller and complete application
is based on Model View and Template. That’s why it is
called MVT application.
 MVT Working Process:-

The Model is the middleware handles data between


the database and the view using the specified
definitions for the data fields. The views receive the
data as well as a request (‘POST’, ‘GET’, etc) from the
client and then accordingly process the data in the
database via the models. This data is available on
the user interface with the help of the templates.
Using the Django Template Language, one template
can be used by multiple views to show different kinds
of data. The templates (or the HTML pages) contain the
data output response from the views and are then
rendered by the browser at the specified url. This is
visible on the client-side while the models and the
views reside on the server-side.
Here, a user requests for a resource to the Django,
Django works as a controller and check to the available
resource in URL.
If URL maps, a view is called that interact with model
and template, it renders a template.
Django responds back to the user and sends a template
as a response.

Difference between MVC and MVT


S.NO. Model View Controller Model View Template

(MVC) (MVT)
1. MVC has MVT has Views for receiving
controller that HTTP request and returning
drives both Model HTTP response.
and View.
2. View tells how Templates are used in MVT
the user data will for that purpose.
be presented.
3. In MVC, we have Controller part is managed by
to write all the the framework itself.
control specific
code.
4. Highly coupled Loosely coupled
5. Modifications are Modifications are easy
difficult
6. Suitable for Suitable both small & large
development of applications.
large applications
but not for small
applications.
7. Flow is clearly Flow is sometimes harder to
defined thus easy understand as compared to
to understand. MVC.
8. It doesn’t involve URL pattern mapping takes
mapping of place.
URLs.

You might also like