Help birds like me understand MVC

Source: Internet
Author: User
Maybe the guy who is new to object-oriented (OOP) programming has always been the kind of mentality that can meet and cannot find the MVC model, when I heard the old birds talk about the MVC model in the sky, I only had to stare at the two eyes and listen silly. Maybe I still don't know what's going on yet, here are some of my younger brother's ideas to help these brothers understand what MVC is like...

So what is the MVC design pattern?
The Model-View-controller (MVC) mode refers to the model-attempt-controller mode. Its core idea is to divide the entire program code into three components that are relatively independent and can work collaboratively, the specific functions are as follows:
Model: business logic layer. Implements specific business logic and status management functions.
View: presentation layer. It is a page for interaction with users. Generally, the data input and output functions are implemented.
Controller: control layer. To control the entire business process, and realize the collaboration between the view layer and the model layer.

At this point, some brothers may have frowned and said, "Fuck, you should be too abstract. if you can understand this, I can understand what I wrote in the book. If you want to play with me, you will say "Y !! ~~", Hey, don't worry. I will give you an actual example to help you continue to understand it.

This MVC design pattern is a bit like the fact that we are going to eat at a restaurant. When you go to a restaurant to eat, the process is like this. First you have to order food. You don't have to shout, it's just a slide of water, the waitress who buried the eight-loggings will be in front of you, "Sir, what do you want? ", Take a look at the recipe. "Give me a whole dish of shredded fish and a bowl of rice ." "Okay, please wait ." Said the waiter trotting toward the kitchen, "that's what... Tell the cook not to put ginger in it !~~"... After a while, the waiter will bring you the food you want. "Please use it slowly ~~ If you need help, call me ." You did not even wash the green peppers in them ~~~

In this simple process, you can use MVC to describe it. You are equivalent to view, the waiter is equivalent to controller, and the damn cook who does not wash green peppers is model. When you first order food with the waiter, it is equivalent to sending a request to the server from our browser. The waiter delivers the food you want to eat to the cook, it is equivalent to the control layer handing over the requests sent by the browser to the business logic layer for processing, and it doesn't matter how the Cook makes this dish at all. Anyway, you can give me a dish of shredded fish, it's his business to wash green peppers. This is the "encapsulation" We often hear from the old bird's mouth. It's good to encapsulate it. You can enjoy the fragrant smell of fish-flavored shredded meat that you don't even wash green peppers ~~ When the waiter puts a hot fish shredded meat in front of you, it is equivalent to sending it back to the browser after the business logic layer completes processing, and then display it to the user through the browser. In this way, a simple MVC coordination interaction is implemented, and you do not know what it is...

Some brothers may have to say again, "If you are talking about MVC, you can just talk about it. What's the food? I just ate fish-flavored shredded meat yesterday. Now I want to vomit !! Hate ~~~", Let's talk about how to apply the MVC in actual projects.

Let's use a simple login module. The requirement is that you enter a user name and password. If the input is the same as the predefined one, the correct page is displayed. If the input is different, the system prompts an error message "you are not here to contact me. Entered incorrect! ".

In this small module, the page for entering the user name and password at the beginning is similar to the page displayed after verification, and here a controller page is required to receive the entered user name and password, there is also a flg returned after verification (this flg is used to determine whether your input is correct and jump to the corresponding page), and a model is missing, then you are the class used for verification. It is used to process whether your input is the same as the predefined one, and then returns a flg. In this way, the separation of logic and page is completely realized. No matter how neat the page is, I will display it, and the Controller does not care whether your model is correct, I gave you the username and password, and you have to give me a flg, just like the waiter, no matter whether the cook or the green peppers are washed, but what about Medol, if you dare to give me a user name and password, I will give you a flg ~~ If you see this, you are a little confused. You can try it later !!

The above are some immature understandings of the younger brother and some experiences of the younger brother in understanding the MVC design model. It may be inaccurate in some places. We also welcome the old birds to criticize and correct them, I sincerely hope that this article will be helpful to the new MVC contact !!

Note: I have not mentioned whether Java or PHP is used, and there are no actual code examples. I think the code should not be a problem if I really understand this process ~~
Transferred from technical learners xuexi5.cn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.