Understanding the MVC pattern.

Understanding the MVC pattern.

What is MVC?

MVC stands for Model-View-Controller and is a popular and widely used software architecture pattern.

Why do we need software architecture?

Software architecture can be considered the blueprint for developing software. It helps us in understanding the structure of software, identifying the different components and their interactions with each other. In short, it gives us a clear picture of software development.

What is a software architecture pattern?

The way we organize the components in software architecture results in a pattern, which is called the software architecture pattern. A popular software architecture pattern that has been commonly used is the client-server pattern.

The client-server pattern

As the name suggests, the client-server pattern has 2 components: the client and the server. The client makes a request to the server who has the resources. The server processes the requests and responds back to the client.

Back to MVC

MVC is just another software architecture pattern like the client-server pattern. Its 3 main components are the model, the view, and the controller.

What do the components do?

The controller controls/handles the request flow. It communicates with both the model and the view components whenever required. The model represents the model of the structure of data that is stored in the database. It handles data logic and is the only component that interacts with the database. The view component is only concerned with how the data is presented to the viewer. It is important to note that the model and the view components never interact directly with each other.

But how do these 3 components work together?

When a request comes, the controller processes it. If the request needs data to be fetched from the database, the controller makes a request to the model, the model gets it done and returns the data. If no such data exists in the database that would mean that an error has occurred. Now, the controller asks the view component to get the view. If the data was fetched, the view will return a file that would render the data. In case of an error, the view will get the error rendered in a file and return it to the controller. Finally, the controller returns the view to the client.