Model-View-Controller (MVC) Presentation
by Michael Dabydeen
github.com/mdabydeen/conestoga-mvc-presentation
What is MVC
The Model-View-Controller (MVC) is an architectural
pattern that separates an application into three main logical
components: the model, the view, and the controller.
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;
}
}
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);
}
}
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());
}
}
Advantages of MVC
- Development of the application becomes fast.
- Easy for multiple developers to collaborate and work together.
- Easier to Update the application.
- Easier to Debug as we have multiple levels properly written in the application.
Disadvantages of using MVC
It is hard to understand the MVC architecture.
Must have strict rules on methods.
Fat Controllers
The Model component corresponds to all the data-related logic that the user works with.
This can represent either the data that is being transferred between the View and Controller
components or any other business logic-related data.
For example, a Customer object will retrieve
the customer information from the database, manipulate it and update it data back to the database or use it to render data.
The View component is used for all the UI logic of the application.
For example, the Customer view will include all the UI components such as text boxes,
dropdowns, etc. that the final user interacts with.
Controllers act as an interface between Model and View components
to process all the business logic and incoming requests, manipulate
data using the Model component and interact with the Views to render the final output.
For example, the Customer controller will handle all the interactions and inputs from the Customer View and update the database using the Customer Model.
The same controller will be used to view the Customer data.