image

MVC Architectural Pattern

3 Apr, 2023 Abdelrahman Etry

What is MVC ?

MVC is an architectural pattern that turns complex application development into a much more manageable process. It allows several developers to simultaneously work on the application.


MVC stands for Model-View-Controller. Here's what each of those components mean:

  • Model: The backend that contains all the data logic
  • View: The frontend or graphical user interface (GUI)
  • Controller: The brains of the application that controls how data is displayed


Why MVC ?

Because of Soc : separation of concerns.

MVC pattern helps you break up the frontend and backend code into separate components. This way, it's much easier to manage and make changes to either side without them interfering with each other.


But this is easier said than done, especially when several developers need to update, modify, or debug a full-blown application simultaneously.


What is Separation of Concerns (SoC) ?

MVC design allows for Separation of Concerns - dividing the logic up between the 3 buckets, so that each bucket can act independently.


The model, view, and controller don’t depend on each other. Why does this matter? Generally, software is worked on by teams - a team might have a designer, engineer, and database architect. Separation of concerns means each team member can work on their piece at the same time, because logic has been separated into buckets. Separation of concerns is also great for maintenance - developers can fix a bug in one piece of code, without having to check out the other pieces of code.


What is Model in MVC ?

This is where you should keep your data model, the algorithms. For example if you write a spreadsheet application, you would keep the data structure of your spreadsheet. You would have the computation engine in your model, you would have the code to save and load your spreadsheet in your model. These model class could potentially be reused in other applications, for example if you have code to do compression of data.



What is View in MVC ?

These are the part of your code to visualize the data (the UI), for a spreadsheet you have a typical spreadsheet view with cells A1 to Z100 etc. You can also visualize your data using a chart view. Etc. A view could be reused in other application as well for example you could reuse your fancy chart view.


What is Controller in MVC ?

What connects the views to the model. This is probably the least reusable piece, the controller know about the model, know which views to displays. Typically the controller will setup the callback that the view will call when the user interact with the app. The controller will then get the information from the model and update the view.


Conclusion:

The most attractive concept of the MVC pattern is separation of concerns.


Modern web applications are very complex, and making a change can sometimes be a big headache.


Managing the frontend and backend in smaller, separate components allows for the application to be scalable, maintainable, and easy to expand.

Recent Posts
image

Laravel Livewire

12 Sep, 2024 Abdelrahman Etry
image

Deployment with Envoyer and Laravel

30 Oct, 2023 Abdelrahman Etry
image

Laravel Request Lifecycle

28 Oct, 2023 Abdelrahman Etry
image

Laravel Queues: Everything You Need to Know

28 Oct, 2023 Abdelrahman Etry
image

Laravel Repository Pattern

28 Oct, 2023 Abdelrahman Etry
image

Laravel Packages You Should Know About

26 Oct, 2023 Abdelrahman Etry