What is MVC Architecture

What is MVC Architecture?

Table of Contents

MVC (Model View Controller) is a design pattern that separates business logic, presentation logic, and data. MVC is used to design both web applications as well as mobile applications. It consists of three layers:

  1. Model: It represents the business layer of the application.
  2. View: It defines the presentation logic of the application.
  3. Controller: It manages the flow of the application and behaves like an interface between Model and View.

What is MVC Architecture?

Model Layer:

  • The Model layer helps in retrieving and storing the state of the model in the database.
  • It contains pure application data.
  • It consists of those classes which are connected to the database.

Example:

public class Course {
       private String CourseName;
       private String CourseId;
       private String CourseCategory;
        
       public String getId() {
          return CourseId;
       }
        
       public void setId(String id) {
          this.CourseId = id;
       }
        
       public String getName() {
          return CourseName;
       }
        
       public void setName(String name) {
          this.CourseName = name;
       }
        
       public String getCategory() {
              return CourseCategory;
           }
        
       public void setCategory(String category) {
              this.CourseCategory = category;
           }
        
    }

View Layer:

  • The View Layer helps in representing the output of the application.
  • It fetches the data from the Model layer and displays the data to the user when the user asked for it.
  • It does not interact with the Model layer directly. It gets the information with the help of the Controller.
  • It also represents data from the diagrams, tables, or charts.
  • It consists of JSP, HTML, etc. and usually, it represents the UI (User Interface) of the application.

Example:

public class CourseView {
       public void printCourseDetails(String CourseName, String CourseId, String CourseCategory){
          System.out.println("Course Details: ");
          System.out.println("Name: " + CourseName);
          System.out.println("Course ID: " + CourseId);
          System.out.println("Course Category: " + CourseCategory);
       }
    }

Controller Layer:

  • The Controller Layer is an interface between the Model Layer and the View Layer.
  • It receives the requests from the user through View Layer and processes them.
  • The request is then sent to the Model Layer for data processing. Once it is processed, the data is again sent back to the View layer so that it can be displayed.
  • It handles user interaction. It interprets the keyboard and mouse events.

Example:

public class CourseController {
       private Course model;
       private CourseView view;
 
       public CourseController(Course model, CourseView view){
          this.model = model;
          this.view = view;
       }
 
       public void setCourseName(String name){
          model.setName(name);      
       }
 
       public String getCourseName(){
          return model.getName();       
       }
 
       public void setCourseId(String id){
          model.setId(id);      
       }
 
       public String getCourseId(){
          return model.getId();     
       }
 
       public void setCourseCategory(String category){
              model.setCategory(category);      
       }
 
           public String getCourseCategory(){
              return model.getCategory();       
       }
       public void updateView(){                
          view.printCourseDetails(model.getName(), model.getId(), model.getCategory());
       }    
    }

Advantages of MVC Architecture:

  1. Navigation control in MVC is centralized.
  2. It becomes easy to maintain the application designed using MVC Architecture.
  3. It provides the reusability of code. The maintenance of code becomes easy.
  4. It helps in avoiding complexity.
  5. It supports Test Driven Development (TDD).
  6. It is SEO (Search Engine Optimization) friendly.
  7. All the objects and classes are independent of each other hence they can be tested separately.

Disadvantages of MVC Architecture:

  1. The framework navigation becomes complex when new layers are added.
  2. Knowledge of multiple technologies is required. A developer should be well skilled in multiple technologies.

Share this article