MVC DESIGN PATTERN
What is MVC?
MVC is an architecture pattern which separates logic, UI & database connection of our application. It’s divided into three broader sections, “Model”, “View” and “Controller”.
1.The “View” is responsible for look and feel of our application.
2.“Model” represents the real world object and provides data to the “View” (If we need from data from database, then we can get data from database).
3.The “Controller” is responsible for taking the end user request and loading the appropriate “Model” and “View”.
The following diagram represents how MVC pattern works internally.
1.Every time, first request comes into controller.
2.Controller decides which action is to be executed and that action goes directly to View or through Model to View by taking data from database
Real Life Example of MVC 5
Suppose there is one restaurant, in this restaurant, some people are working as follows:
This man gives an order to the appropriate cook for getting the order ready.
1.Guy who is an expert cooks work in the kitchen
2.Guy (Manager) who takes order from customer & writes down that order on a small paper with appropriate table number, i.e., one.
3.Now some customers come into this restaurant, the guy who is going to take an order shows a menu card of this restaurant to those customers.
4.Customer sees this menu card. For example, customer sees items likes Poha, Upma, Dosa, etc. Now that customer would like to order Poha or dosa, then the customer can imagine how Poha or Dosa looks like.
5.After taking order from customer, that guy (Manager) give this small purchi to a man who is seating outside the kitchen. This man who is seated outside the kitchen acts like a Controller in MVC, means every order is handled by this controller.
6.The cook sees the order & tries to make the food ready, if he needs some extra items to make food like for Dosa, he needs to make 1 Chatani from coconuts, then cook, visit to fridge & take the coconuts & make chatani ready by adding water, sugar, etc. In this scenario, the cook is taking data from database means cook is acting like Model in MVC.
7.After some time, the order of that customer is ready. Now the man seated outside the kitchen calls the waiter & tells him to serve this order to that customer who has ordered from table number one.
8.Now waiter adds some items to make better look & feel of food like he adds some...
9.Now customer order is ready to eat. This is the actual View in MVC. Waiter is responsible for making decorate view).