Model View Controller

Model View Controller

MVC

MVC - AKA - Model View Controller

  • MVC is not a new concept, and it's a pattern that you see in your daily life.
  • It is also not specific to a one programming language, It is a way how well you organize your code.
  • It is not specific to a ruby, php, .net or Java or for a Swift.

Advantages of using MVC

  1. You can write the dumb Code and more the code is going to be dumb, it's gonna be more beneficial.
    • All I am trying is to say that we are here trying to reduce the dependency of each code between 2 files.
    • All we want is if some data is passed on to a particular file from another file, it just proccess the data, it shouldn't be worried about from where this data is coming up from or there are any type of dependencies between any files with code related or something.
  2. It just needs the data and also fetching the data that's all.

MVC Model Explained in real scenario

USER (1600 × 840px).png

In this example we can clearly see that our cook is not worried about who is ordering the food.Cook is not worried whether they are male, female, kid maybe a gradpa. That is actually a dumb way of separating the people with each other.

  • From the above diagram, Lets assume that you went to Restraunt.
  • Now, you can't go to the kitchen and make food by yourself. You just wait for the waiter to come at your table for taking order.
  • Whenever you tell the waiter what you want he just gets back to you with stuffs that you ordered.
  • After taking order from you waiter moves to the kitchen.
  • Now in the kitchen waiter is not going to prepare food for you, the food is prepared by the cook.
  • Now, waiter will ask for the food from the cook on behalf of your order.
  • Now, Cook will need some ingradients for the preparation of the food for that he will take out items from the fridge like for the preparation of the omelette he will need EGGs, tomatoes etc from the fridge. And now cook will prepare the Food.
  • After that cook will handover the food to the Waiter and probably the cook might handover other foods also like SodaLime, coca-cola etc
  • Now its the job of the waiter to move those food outside the kitchen now waiter is aware what kind of food needs to be served to you and how it should e served.
  • At last Waiter arrives to you with your orders.

USER (1600 × 840px) (1).png now just assume that

  1. You as a guy is actually a View .
  2. The Waiter guy is actually a Controller.
  3. Cook guy is a Model.
  4. Fridge guy is actually a DATA.

So this is a cool example you have been visiting Restraunt everyday and they are following the rules of MVC throught every year.


In some places it may be opposite of that like for example if there is a store which just sells the Ice Creams now it would be bad approach if we use Model View Controller there because there is just one job to do that is to just bring up the ice cream from the fridge and serve it thats all.

Point of time where MVC is used

  1. In most of the cases where the application is big and humongous, then it's a good idea to use MVC approach.

Your model is not your data, its the seprate thing. The data needs to be handled somewhere that is what cook is doing in the above example (diagram).

  1. In real life example your data would be something like fetching up from an API request or may be from the firebase or wherever you guys fetching your data and your models should be the guy who should process ( cook up ) process the data and finally your controller should be responsible from taking the data, filtering the request for example -
    • Let say if you have asked for a result of maybe top 10 wonders of the world that belong to ancient category, now there can be thousands of thousands wonder of the world now it's the job of controller to filter out the data and just produce that on to the view and the job of the view is to simply arrange those data which has been passed on to the controller.