The MVC pattern

The MVC pattern is a way of working that makes it possible to differentiate and separate what the data model is (the data that the app will have that is normally stored in DB), the view (an HTML page), and the controller (where the requests of the web app are managed).

The MVC is a pattern for software development that is based on separating the data, the user interface, and the logic of the application. It is mostly used in web applications, where the view is the HTML page, the model is the database manager system and the internal logic, and the controller is responsible for receiving the events and solving them. Let's explore each element in more detail:

  • Model: This is the representation of the information in the system. It works with the view to show the information to the user and is accessed by the controller to add, delete, consult, or update data.
  • View: This is presented to the model in a suitable format so that the user can interact with it. In most cases, it is the graphical user interface.
  • Controller: This is the most abstract element. It can receive, process, and respond to events that are sent by the user or by the application itself. It interacts with both the model and the view.

For a detailed understanding for the use of the MVC model, let's look at its control flow:

  1. The user activates an event in the interface (for example clicking on button, link)
  2. The controller receives the event and manages it
  3. The controller consults or modifies the model
  4. The controller sends the response to the interface and it reacts depending on it (changes the screen, opens a link, and so on)
  5. The interface waits for a new user action

In the following diagram, we can see the steps we just described:

Among the advantages that this pattern provides us, we can highlight the following:

  • It is focused on separating responsibilities: Let's think about how current applications and websites are created; that is, HTML is used for the visual components, CSS is used for the style, and Javascript is used for the logic, each with its own approach and its own responsibility. The concept is the same for MCV including the components that we mentioned before.
  • It reuses code: Any framework that's created from MVC allows you to reuse code and return total or partial views, avoiding duplicating styles or content in the views. All of the data handling is done in the models, so if you modify your database, it is only necessary to modify the corresponding model so that it can handle the updated data, without the need to update each place where it was used.
  • We avoid spaghetti code: With this design pattern, we can reduce and even eliminate the use of server and presentation code in one place.
  • Perfect for multidisciplinary teams: With this design pattern, we can have teams where each person deals with a certain layer. For example, we can have someone in charge of designing the application and someone else in charge of creating the business rules and other activities. Each person can work independently of the other without suffering affectations.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset