MVC Design Pattern - Java Example

By MVC Design Pattern - Java Example

In the previous tutorial, I explained MVC vs MVVM. We learnt ha every software application has its own ways in which they are designed. MVC simply stands for model view controller and it is one of the commonly used design patterns. In this tutorial, we shall learn how to write a program in Java while following the MVC pattern.

MVC design pattern is used to separate the application’s concerns i.e. the model and view.

Model – this represents an object carrying data, commonly known as Java POJO. It has logic to update the controller whenever its data values change.

View – this represents the user interface or the visualization of data that models contain.

Controller – this acts as an interface between view and model. It intercepts all incoming requests and controls the data flow into the model and updates the view when data changes. This helps keep the view and model separate

MVC – Java Example

Let’s now create a real application in the MVC design pattern. We will create a Car object to act as the model. Then we shall create a CarView class to act as the view. The CarView contains a method to print car details on the screen. Then we will have CarController class that is used to store data in Car model and update the view accordingly. 

We will finally create a demo class to demonstrate how the application works.

Car Application Class Diagram

The following is a quick class diagram of our car application.

MVC JAVA Project

Step 1: Create a Model

The following is the content of Car.java file:

package car;
public class Car {
    private String make;
    private String model;
    private String color;
    private String reg_no;
    private int year;
    public Car(String make, String model, String color, String reg_no, int year) {
        this.make = make;
        this.model = model;
        this.color = color;
        this.reg_no = reg_no;
        this.year = year;
    }
    public Car() {
    }
    public String getMake() {
        return make;
    }
    public void setMake(String make) {
        this.make = make;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getReg_no() {
        return reg_no;
    }
    public void setReg_no(String reg_no) {
        this.reg_no = reg_no;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    @Override
    public String toString() {
        return "Car{" + "make=" + make + ", model=" + model + ", color=" + color + ", reg_no=" + reg_no + ", year=" + year + '}';
    }
  
}

Step 2: Create a View

The following is the content of CarView.java file:

package car;
public class CarView {
    public void displayCarDetails(Car car){
      System.out.println("================================================");
      System.out.println("Car: ");
      System.out.println("Make: " + car.getMake());
      System.out.println("Model: " + car.getModel());
      System.out.println("Color: " + car.getColor());
      System.out.println("Reg Number: " + car.getReg_no());
      System.out.println("Year: " + car.getYear());
      System.out.println("================================================");
   }
}

Step 3: Create a Controller

The following is the content of CarController.java file:

package car;
public class CarController {
    private Car model;
    private CarView view;
    public CarController(Car model, CarView view) {
        this.model = model;
        this.view = view;
    }
    public String getCarMake() {
        return model.getMake();
    }
    public void setCarMake(String make) {
        model.setMake(make);
    }
    public String getCarModel() {
        return model.getModel();
    }
    public void setCarModel(String modelName) {
        model.setModel(modelName);
    }
    public String getCarColor() {
        return model.getColor();
    }
    public void setCarColor(String color) {
        model.setColor(color);
    }
    public String getCarReg_no() {
        return model.getReg_no();
    }
    public void setCarReg_no(String reg_no) {
        model.setReg_no(reg_no);
    }
    public int getCarYear() {
        return model.getYear();
    }
    public void setCarYear(int year) {
        model.setYear(year);
    }
    
    public void updateView(){
      view.displayCarDetails(model);
   }
}

Step 4: Test the application

The following is the code for MVC_DEMO.java file:

package car;
public class MVC_DEMO {
   private static Car fetchCarData(){
      Car car = new Car("Toyota", "Prius", "White", "Pri Toy", 2015);
      return car;
   }
    public static void main(String[] args) {
      //fetch car details from a database or file or where stored
      Car model  = fetchCarData();
      //We create a view. The view will print car details on the screen
      CarView view = new CarView();
      CarController controller = new CarController(model, view);
      controller.updateView();
      //update model data
      controller.setCarReg_no("KCA 132L");
      controller.updateView();
   }
}

Results

The following are the results after running the program:

run:
================================================
Car: 
Make: Toyota
Model: Prius
Color: White
Reg Number: Pri Toy
Year: 2015
================================================
================================================
Car: 
Make: Toyota
Model: Prius
Color: White
Reg Number: KCA 132L
Year: 2015
================================================
BUILD SUCCESSFUL (total time: 0 seconds)

Conclusion

Large applications like Facebook and Youtube require MVC pattern. This tells that with MVC skills you will be very marketable in the programming world. You need to follow the best practices of MVC to master the concepts in detail.

You might be interested in knowing what is MVVM (Model View ViewModel) design pattern, this article, MVC vs MVVM has it in details. Kindly read.

Applause for you being so patient and curious! Please share the article with your friends and fellow Java developers

Was this article helpful?
Donate with PayPal: https://www.paypal.com/donate

Bessy
Eric Murithi Muchenah

Life is beautiful, time is precious. Make the most out of it.