Explore the Beginner MVC Model: Structures and Examples

Tram Ho

What is MVC pattern?

The Model-View-Controller (MVC) pattern is an architectural pattern that separates an application into three main logical components, Model, View, and Controller. Hence MVC abbreviation. Each architectural component is built to handle the specific developmental aspect of an application. MVC separates the business logic layer and the display layer separately. Today, the MVC architecture has become ubiquitous for designing web apps as well as mobile apps.

When was MVC born?

  • The MVC architecture was first discussed in 1979 by Trygve Reenskaug.
  • The MVC model was first introduced in 1987 using the Smalltalk programming language.
  • MVC was first accepted as a general concept, in a 1988 paper.
  • In recent times, the MVC pattern is widely used in modern web applications.

MVC architecture!

Key MVCs include:

  • Model: It includes all of its data and related logic.
  • View: Present data to the user or handle user interaction.
  • Controller: As the most important part in the model, it connects the Model and View parts.

View

  • The view is the part of an application that represents the presentation of data.
  • The view is made up of data that we get from the data in the model. A view asks the model to provide enough data for it to display output to the user.
  • View is the place that contains interfaces such as buttons, input boxes, menus, images … it is responsible for displaying data and helping users interact with the system.

Controller

  • The controller is the part of an application that handles user interactions. The controller interprets mouse and keyboard input from the user, informing the model and view to change as appropriate.
  • Controller is the place to receive processing requests sent from users, it will include classes / functions that handle many business logic to help get the correct data information needed by the business class Model to provide and display data. Data it out to the user thanks to the View class.
  • Controller sends commands to the model to change its state (For example, we add a new user or update a user name). The controller also sends commands to its related view to change the view of the view (For example: viewing a user’s information).

Model

  • The model component stores data and its associated logic. These include function classes that handle tasks such as querying, adding, editing, or deleting data. For example, a Controller object will retrieve customer information from the database. It manipulates the data and sends it back to the database or uses it to display data.

Interaction between components

  • Controller interacts with the View.
  • Controller interacts with the Model.
  • Model and View do not have direct interaction, but it interacts with each other through Controller.

Example of interaction: When the user presses login from the view, the request will be sent from the browser to the controller, the controller will call the logic processing model and return the result to the user through the view.

MVC example

Take a look at the Model View Controller example from our daily life:

  • Let’s say you go to a restaurant. You won’t have to go into the kitchen and prepare food that you can definitely make at home. Instead, just go there and wait for the waiter to arrive.
  • Now the waiter comes to you, and you just have to order the food. The waiter doesn’t know who you are and what you want, he just details your food order.
  • After that, the waiter moved to the kitchen. In the kitchen, the waiter does not prepare food for you.
  • The chef prepares the food for you. The waiter will give him your item along with your table number.
  • Cook the food then prepare it for you. He uses the ingredients to cook the food. Let’s assume that you order a vegetable sandwich. Then he needs the bread, tomatoes, potatoes, bell peppers, onions, a little bit of cheese, etc. that he gets from the refrigerator.
  • Finished cooking, then deliver the food to the waiter. Now it is the waiter’s job to move the food out of the kitchen.
  • Now the waiter knows what dishes you ordered and how they were served.

After viewing this example we can see:

  • View: It’s you.
  • Controller: The server.
  • Model: A chef.
  • Database: is the refrigerator.

Advantages of MVC:

  • Easy to maintain code, easy to expand and develop.
  • Easier support for new customers.
  • Development of different components can be done in parallel.
  • It helps you avoid complications by dividing the application into three units Model, View and Controller.
  • Provides the best support for test-driven development.
  • It works well for Web applications supported by large groups of web designers and developers.
  • Provides the ability to clearly separate concerns.
  • Search Engine Friendly (SEO).
  • All objects are classified and the object is independent of each other so you can examine them separately.

Disadvantages of using MVC

  • It is difficult to read, modify, test and reuse this model.
  • There is no official authentication support.
  • Increased complexity and inefficiency of data.
  • Difficulty using MVC with modern user interface.
  • It takes many programmers to do parallel programming.
  • Knowledge of many technologies is required.
  • Maintain a lot of code in the Controller.

Conclude

  • MVC is an architectural pattern that separates the application into 1) Model, 2) View, and 3) Controller.
  • Model: It includes all of its data and related logic.
  • View: Present data to the user or handle user interaction.
  • Controller: Is the connection between the components Model and View.
  • The MVC architecture was first discussed in 1979 by Trygve Reenskaug.
  • MVC is a model that is easy to extend and maintain code.
  • Some popular MVC Frameworks are Rails, Zend Framework, CodeIgniter, Laravel, Fuel PHP, etc.

Reference link: https://www.guru99.com/mvc-tutorial.html

Share the news now

Source : Viblo