Monday, 3 August 2020

What is MVC (Model View Controller)

What is MVC (Model View Controller)


What is MVC (Model View Controller) 

-> This is not a new concept first of all and it is a thing that you see in your daily life as well I'll prove that in example just in a moment and also this is not specific to one programming language

-> It is a way of how you organize your code it is not like a specific thing for Ruby or PHP or for Java or for Swift it's just a way of how you organize your code and trust me you also see this kind of pattern every single day in your life

-> I can do a little bit better job in explaining what model-view-controller so yes you have seen model-view-controller probably a hundreds of time in your day-to-day life now

-> Let me try to explain what the model-view-controller is and where you see in your daily life so let's just see that this is you now you go to a restaurant and let's just assume this white entire animation kind of thing is a restaurant

-> You move on to a restaurant now what you do at a restaurant you don't just go to the kitchen and make a food by yourself you can do that at your home so what you wait you just go there and you wait for a waiter to come on now waiter comes to you

-> You just order the food to the waiter now waiter is not aware who you are or what you want to have it just gets that from you now whenever you sell the waiter that yes I want this the waiter just gets the order

-> Now waiter moves to the kitchen now in the kitchen waiter is not going to prepare your food, the food is prepared by the cook so let's bring up the cook now waiter will tell that the cook hey this is a guy out there

-> He wants this kind of food and now cook is the guy we're going to prepare the food for you but cook did not prepare the food magically he needs some ingredients to cook the food he'll open up the fridge

-> We'll put out something legitly you order an omelet so he'll get the egg sugar and whatever it needs so let's bring our fridge as well so now the fridge is there and cook will cook the food now cook

-> Will finally hand over the food to the waiter and probably the cook might hand over a couple of other foods as well like maybe a glass of soda or maybe my favorite icy or omelette that you have ordered now it's the job of the waiter to move those food outside the kitchen now waiter is actually aware

-> What kind of foods need to be served to you and how it should be served so waiters comes back to you and order and your order is now served so you might be wondering hey what this tool is all about and probably you might have anything guess that

-> This is all of your model-view-controller so this you guys is actually have you this waiter guy is actually the controller and yes you get that right this hood guy is model in itself and if you haven't guessed that yes the fridge guy is actually the data

-> So how about that this is a cool example and this is kind of a thing you see every day you have been going to restaurant every day and they are following a model-view-controller objectives throughout the year

-> So this is basically your model-view-controller now I can understand you have gone through with that restaurant example pretty easily and nicely now you want to understand how this is applied in the code

-> Now first of all a lot of people believe that model-view-controller is a thing for only dotnet or may be only for Ruby or may be hungry for PHP but it's not it's the design pattern it's the way how you arrange your code

-> It's going to be possible in everywhere you might want to write an application and PHP Swift java.net Ruby Python you can just arrange your code like that now the whole advantage of this approach is you can write dumb code

-> The more the code is going to be dumb it's going to be more beneficial what I mean by that now all I'm trying to say is we are trying to reduce the dependency of each code in this file from each other

-> We want that if some data is being passed on this file it just process this data it shouldn't be worried about from where this data is coming up ROM or there shouldn't be any dependencies

-> Wherever I does that I always recommend to use the model-view-controller approach but let me give you also an example that this kind of approach although is good in most places but in some places it can be opposite of that

-> Like for example if there is a store which just sells the popsicles or ice creams it would be really bad approach if we use model-view-controller there because there is just one job to do just bring up the ice cream from the fridge and serve it that's it if you use it if you will use the model-view-controller approach there

-> It would be overused and overkill of the resources so at some point of that time model-view-controller is not a good approach but in most of the cases where application is pretty humongous pretty big then it's a good idea to use the model-view-controller approach

-> Now although we are not going into the in depth of what this model what is view what is controller because if I will be taking an example specifically for Android it would be really not a good thing for iOS if I will be taking an example directly from iOS it would not be a good approach for Android

-> So all I'm doing here is trying to generalize the thing as much as possible now one thing I would like to mention that a lot of people misunderstood the thing that your model is not the data again

-> I repeat your model is not the data the data is completely separate it needs to be cooked up so that is what the cook is doing in our example

-> In the real life example your data would be something fetching up from an API request or maybe from the firebase or wherever you guys fetching your data and your models should be the guy who should cook up these data should process the data

-> Finally controller should be responsible from taking the data filtering the request and let us say you have asked for a result of maybe top 10 wonders of the world

-> There can be hundreds of such wonders of the world now you are looking only for the ancient wonder of the world so it's the job of controller to filter out the data and just produce that on to the view.

Dinesh Wadhwani

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment