Sobes.tech
Junior

What is the architectural pattern MVC?

sobes.tech AI

Answer from AI

MVC (Model-View-Controller) is an architectural pattern that divides an application into three interconnected parts to improve code organization and facilitate development and maintenance:

  • Model: Represents data and business logic. Responsible for data retrieval, storage, processing, and rules governing these data. It has no direct view of the user interface.
  • View: Responsible for displaying data to the user. It gets data from the Model and visualizes it. It contains no business logic and does not interact with data directly, except for rendering.
  • Controller: Acts as an intermediary between the Model and the View. Handles user interactions (e.g., clicks, data input), retrieves data from the Model, passes it to the View for display, and updates the Model when data changes are initiated by the user.

Main relationships:

  • The user interacts with the View.
  • The View passes user actions to the Controller.
  • The Controller processes user actions, interacting with the Model (requesting or updating data).
  • The Model notifies the Controller (or directly the View in some variations of the pattern) about data changes.
  • The Controller updates the View based on changes in the Model (or the View updates itself when directly interacting with the Model).

Advantages:

  • Separation of concerns: Simplifies development, testing, and maintenance of individual components.
  • Code reuse: The Model and Controller can be used with different Views.
  • Easier testing: Components can be tested independently.
  • Parallel development: Different teams can work on different parts (Model, View, Controller) simultaneously.

Disadvantages:

  • Increased complexity: May be excessive for small applications.
  • "Fat Controller" problem: The Controller can become too large and complex if principles of separation are not followed.
  • Difficulty in defining boundaries: Sometimes it is hard to clearly define where one component ends and another begins.

Example (schematic):

// Model (e.g., a class for data handling)
class UserModel {
  constructor() {
    this.users = [{
      id: 1,
      name: 'Alice'
    }, {
      id: 2,
      name: 'Bob'
    }];
  }

  getUsers() {
    return this.users;
  }

  addUser(user) {
    this.users.push(user);
    // Notify about change (in a real app - via events)
  }
}

// View (e.g., a function to render the list)
function renderUserList(users) {
  const list = document.getElementById('user-list');
  list.innerHTML = users.map(user => `<li>${user.name}</li>`).join('');
}

// Controller (for handling events and coordination)
class UserController {
  constructor(model, view) {
    this.model = model;
    this.view = view;

    // Initialize view
    this.updateView();

    // Handle events (e.g., button click)
    document.getElementById('add-user-button').addEventListener('click', () => {
      const newUser = {
        id: this.model.getUsers().length + 1,
        name: prompt('Enter user name:')
      };
      if (newUser.name) {
        this.model.addUser(newUser);
        this.updateView(); // Update view after model change
      }
    });
  }

  updateView() {
    const users = this.model.getUsers();
    this.view(users);
  }
}

// Usage
const model = new UserModel();
const controller = new UserController(model, renderUserList);

This is a simplified example demonstrating the basic principle. More advanced variations of this and other architectural patterns are used in real frameworks (such as Angular, React, Vue).