visit
Every app is composed of many layers (connection, interaction, utilities, etc). For that reason, it’s important to define the layers that you’ll be using in your app and how they are going to communicate with each other before actually building it.The MVC architecture aims at giving us a well-defined design pattern to build our app upon, already establishing all the layers and connections between them.
In the beginning, the MVC was developed to map the input, processing, and output of programs with a GUI (Graphical User Interface).The majority of OOP (Object Oriented Programming) applications can benefit from the MVC pattern. Using such a pattern will make the code more reusable and enhance the interface definition since each layer takes care of its tasks. In practice, if you need to change or implement a feature in your code at some point, this change/implementation would be much more flexible and straightforward with MVC, avoiding big changes and refactorings in your code.
Understood… But how can I implement this in my application and become a better developer?We’re getting there! To elucidate this, let’s use the illustration below to map the MVC behavior.MVC’s components and their relations to each other.Let’s picture this scenario…We’ve built an app that has only one button. This button, when pressed, shows user data like name, email, and age. Now, let’s figure out the MVC structure of this app:
When the user presses the button on the screen, the View notifies the Controller who triggers the show_user method. To display the user data, we need to create a User object to retrieve it to the Controller.
Therefore, the Controller notifies the Model (the User class in this case) who gets the data from the database and returns the User object to the Controller.
The Controller checks if everything is ok with that object, and, if so, output the result to the View that updates to allocate the data of the User object on the table and display it for the user that pressed the button.
If you’re still having trouble to grasp the concept, fear not! It takes time to build a fair understanding of MVC architecture. To make it easier, we’ll now see a detailed explanation of each layer.We can store a lot of information in this object, like color, model, year of production and even the car’s mileage. We can store anything we want into it, but remember: the way this data will be displayed should not be addressed in the model object.For this, we’ll use the View layer.Notice that models are a bit flexible and its assignments might depend on the specific app that you’re building, but in most cases, the model should not bother with what will be presented in the View. The core task of the Model is to manipulate relevant data for the good functioning of the app.
Here, our core task is to display data and ignore how this data is stored or processed. Once again, keep in mind that these definitions aren’t absolute and you might encounter special cases in which you’ll need to store some data in the View, but rest assure that this isn’t the common convention.The View must make sure that it is displaying the Model object correctly. As a consequence, it will need to know when the object is edited or deleted. Since the View and the Model layer don’t communicate directly, we need a middle man up to the task. This middle man is called Controller.
That would trigger a Controller method that would request data from the User Model, retrieve that data and update the View with it. The Controller is the manager that keeps everything in place.When all the communication between the View and Model are set trough the Controller, you make your code more reusable, recycling methods that can be used to set communication between multiple pairs of Views/Models.