6.1. Model-View-Controller (MVC)

Maheshmaddi
3 min readApr 11, 2023

--

The Model-View-Controller (MVC) pattern is an architectural pattern that separates the concerns of data management, user interface, and user input control. It consists of three main components:

  • Model: Represents the data and business logic of the application.
  • View: Represents the user interface and displays the data from the model.
  • Controller: Handles user input and updates the model and view accordingly.

Here’s a simple example of the MVC pattern in Java:

// Model
class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

// View
class PersonView {
public void printPersonDetails(String personName) {
System.out.println("Person: " + personName);
}
}

// Controller
class PersonController {
private Person model;
private PersonView view;

public PersonController(Person model, PersonView view) {
this.model = model;
this.view = view;
}

public void setPersonName(String name) {
model.setName(name);
}

public String getPersonName() {
return model.getName();
}

public void updateView() {
view.printPersonDetails(model.getName());
}
}

// Client code
public class MVCPatternDemo {
public static void main(String[] args) {
// Create model and view
Person model = new Person();
PersonView view = new PersonView();

// Create controller and bind model and view
PersonController controller = new PersonController(model, view);

// Set person name and update the view
controller.setPersonName("John Doe");
controller.updateView();
}
}

In this example, the Person class represents the model, which stores the person's name. The PersonView class represents the view, responsible for displaying the person's details. The PersonController class is the controller that handles user input, updates the model, and refreshes the view.

The client code creates instances of the model and view, binds them to a controller, sets the person’s name, and updates the view to display the person’s details.

Use Case: Online Bookstore with MVC Pattern

Class Diagram for Online Bookstore MVC Pattern

In this real-time use case, we will create a simplified online bookstore application using the MVC pattern in Java. The application will display a list of books and their details. We will have the following components:

  1. Model: Book.java
  2. View: BookView.java
  3. Controller: BookController.java
  4. Main class to run the application: OnlineBookstore.java

Model — Book.java:

public class Book {
private String title;
private String author;
private double price;

public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}

// Getters and Setters
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}

View — BookView.java:

public class BookView {
public void printBookDetails(String title, String author, double price){
System.out.println("Book: ");
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Price: $" + price);
}
}

Controller — BookController.java:

public class BookController {
private Book model;
private BookView view;

public BookController(Book model, BookView view) {
this.model = model;
this.view = view;
}

// Methods to interact with the model
public String getBookTitle() { return model.getTitle(); }
public void setBookTitle(String title) { model.setTitle(title); }
public String getBookAuthor() { return model.getAuthor(); }
public void setBookAuthor(String author) { model.setAuthor(author); }
public double getBookPrice() { return model.getPrice(); }
public void setBookPrice(double price) { model.setPrice(price); }

// Method to update the view
public void updateView() {
view.printBookDetails(model.getTitle(), model.getAuthor(), model.getPrice());
}
}

Main class — OnlineBookstore.java:

public class OnlineBookstore {
public static void main(String[] args) {
// Create a book instance
Book book = new Book("Effective Java", "Joshua Bloch", 39.99);

// Create a book view instance
BookView view = new BookView();

// Create a book controller instance
BookController controller = new BookController(book, view);

// Display the initial book details
controller.updateView();

// Update the book details and display the updated details
controller.setBookTitle("Clean Code");
controller.setBookAuthor("Robert C. Martin");
controller.setBookPrice(34.99);
controller.updateView();
}
}

When you run the OnlineBookstore application, it will display the book details, update them, and display the updated details using the MVC pattern.

Note: For complete list of design patterns click here

--

--