Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • MVC, why and what? An analogy

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 410
    Comment on it

    In medieval period of programming, when I use to read what Yashavant Kanetkar wrote about C language, I wonder if this high-level programming language pattern can be bettered. It gives me so much freedom to write code and I really-really liked the structural way of programming and wrote many programs (even interesting one) but then C++ ruined it all.

    Recently, when one of my friends asks me "What the heck is this MVC? Why do I have to follow this software pattern??" He was furious and back came the memory when I had to leave my old structural programming method to a mature one. Thanks to good food, thats the only thing keeping my friend from really bad mood and me being blunt.

    I tried to persuade him "Back in old days when we were all 'Coding Gorillaz', we wrote lousy code that run in desired way and don't give a damn about designing pattern."

    I got back-fired immediately "For your general information code were written for many drivers and UNIX kernel using structural programming language".

    "Yes thats very true, but those were the good old days, now as we are evolving from coding gorillas to human, we look to solve the problem in as human way as possible. When Coding Gorillas look to a problem as a giant algorithm we try to identify and separate tasks to find a beautiful solution. Now, we want The End of the problem permanently and make the solution expandable, so that, if in future any unforeseen problem erupts we could just work on that part of the Giant algorithm instead of scratching our head like a Gorilla.

    Like we have separate set of files for JavaScript, style-sheet and html, these fragments works independently and we can work in these separately like coding Gorillas. Deep inside we are all coding Gorillas".

    So, what's MVC?

    "MVC is software pattern where we have three main components that talk to each other and at a same time they work independently. This happen in real life too like the eating place we are in.

    Consider this eating place where people of different taste comes to eat, what if there's only one guy who take orders, cooks food and manage accounts etc. etc.

    alt text

    This would have worked, our panwala is a good example but he has limited stuff to sell.

    This restaurant owner decides to go for more humanly solution.

    First, he gets a guy who can cook decently and had to concentrate only on cooking and not anything else. Second, he gets a drop dead gorgeous girl who had a voice of Lauren Bacall (and right now having most of your attention), for taking orders.

    Here's, the program that works perfectly.

    Starving people come here, Waitress talk to them and write something in a piece of paper. She passes this to the cook who prepares dishes in his kitchen one by one, after the dish is ready waitress bring it to us. All People Happy!!

    Now, consider a scenario of a bigger place, where there are more hungry people with never-ending appetite. Here the orders will be much more and our cook may get frustrate and decide to open a tea-stall instead.

    So what do we need next? Of course more cooks and dont forget to bring more beautiful waitress.

    Now, since we have more cooks and waitress, we also need a good manager so that cook and waitress just have to concentrate on their jobs and not managing other stuff.

    Here are the modifications in our old program.

    Smiling waitress will go to the people, take their orders, write it in a piece of paper along with a table number and pass it to the manager, she forgets about everything else and return to take more orders. Manager will check the order and pass it to the cook accordingly.

    Orders like Dosa will go to the south-Indian cook and Lassi's order will go to Punjabi cook. The cook will then works on their part (while discussing Sachin's retirement) and pass the cooked items to the manager. Manager, will then assemble the items according to the table and pass it to the Waitresses and thus All people will be Happy!! Again!!

    Furthermore, whenever Restaurant needs to include Chinese dishes in the menu, they will hire one more cook who knows how to make momos. Taking feedbacks from hungry people, cooks can also learn effective ways and increase productivity.

    If theres any issue in food we can check it with the cooks. Now, It will be easier to know which cook needed the improvement and in which dish as different cuisine have different associated cooks.

    I assume you probably get by now that Cook is Model, Waitress is View and Manager is Controller of MVC architecture.

    This is what I can draw with MS Paint for explanation.

    alt text

    If this restaurant analogy seems pretty easy then let me warn you beginners, when you try your first MVC implementation, you may suck."

    I will show you the code implementation in second (code) part of this writing.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: