MVC Pattern

This lesson discusses the MVC pattern in detail with the aid of a coding example.

What is the MVC pattern?

The MVC pattern stands for model view controller pattern. It is an architectural pattern used to organize the code of your application. It consists of three components:

  • Model:

    This is the model component that manages the data that the application may require.

  • View:

    The view is used for the visual representation of the current model. It renders data on the user’s side.

  • Controller:

    The controller connects the model and the view components.

The model is independent of the view, meaning, it is not concerned with the user interface and how the information is displayed on the user side. The view, on the other hand, is an observer of the model. Whenever the model gets modified (data is updated), it notifies its observer (the view) which then reacts accordingly.

As mentioned, view is the visual representation of the model. Whenever it is notified of a change in the model, the view updates correspondingly. As the view layer is what the users get to see, this is also the layer they get to interact with, such as editing or updating attribute values.

The controller is the connection between the model and the view. The controller takes input from the user such as a click or keypress which updates the view side and then updates the model. It can sometimes update the view directly as well.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.