MVC 另类解释
Dec 22, 2019 21:00 · 1389 words · 3 minute read
译文
Model-view-controller (MVC) 框架是现代 web 应用程序开发中很重要的一部分。身处 web 开发者之中,你可能满耳朵都是 Ruby on Rails 啊 Angular 啊 Django 什么的。
更一般而言,MVC 逻辑可用于描述几乎所有使用像 PHP、Ruby、Python 或 JavaScript 之类的语言来进行 web 开发的过程。
然而。。。
许多 web 开发者面带微笑在这个神秘的世界披荆斩棘。当某个资深开发者或团队需要查看一下他们的代码,他们就会大谈常见的编程实践。
实际上,现代 web 开发中的 MVC 模式可以通过在吧台点一杯饮料来比喻。是的,这意味着如果你去过酒吧,那就能理解所有 web 应用程序共享的主要架构。(那像我这样没去过的怎么办。。。)
什么是 MVC 模式?
- 模型(Model):以可靠的形式结构化数据并根据控制器的指令进行操作
- 视图(View):基于用户的操作以简单直观的方式向用户展示数据
- 控制器(Controllers):接收用户的命令,将其发送到模型以更新数据,向视图发送指令来更新界面
或者,以图表形式:
无聊,回酒吧。
当一个菜鸟程序员走进酒吧。。。
在周五的夜晚你来到了酒吧,挤到吧台。此时酒吧已经人满为患,你穿过人群最终引起了服务员的注意,然后你喊一句,“来杯曼哈顿!”
你就是用户,你喊的那句就是用户请求。对于你来说,曼哈顿只是你想要的饮料,而且你清楚它的味道。
服务员向你点头,对于他来说,曼哈顿并不单单是饮料,而是一顿操作:
- 拿个玻璃杯
- 倒点威士忌
- 倒点苦艾酒
- 苦化
- 搅一搅
- 加个樱桃
- 收费
服务员是控制器,只要你用他们听得懂的语言说出“曼哈顿”一词,开始忙活。这项工作本质上类似于制作玛格丽塔酒或草莓酒,但用了永不混淆的独特配方。服务员只能用吧台后面的工具和资源。这个有限的工具集就是模型,包括:
- 服务员的手
- 搅拌设备
- 酒
- 混合剂
- 玻璃杯
- 装饰品
在高级的酒吧里,他们可能还有机器人助手!或者自动化搅拌设备。这无关紧要,总之调酒师只能使用可用资源。
最终你看到并消费的成品饮料就是视图。视图是基于模型的有限选项构建的,并由控制器(服务员)安排和传送。
原文
If you have been to a bar, then MVC ain’t that hard.
Model-view-controller (MVC) frameworks are a crucial part of building modern web applications. Walk into a room of web developers, and you will likely be bombarded with mentions of Ruby on Rails, Angular or Django.
More generally, MVC logic can be used to describe almost any web development process that uses a language like PHP, Ruby, Python or JavaScript.
Never the less…
Many web developers navigate this mysterious world by hacking through the weeds with a smile on their face. When a senior developer or teammate needs to look at the code from one of these developers, they will give an immediate yelp, followed by a swift lecture on common coding practices.
This is no way to go through life! In fact, the MVC pattern in modern web development can be easily explained by ordering a drink from a bartender. And yes, that means if you have been to a bar, then you can understand the major structural pattern shared by all web apps.
What is the MVC Pattern?
- Model: Structures your data in a reliable form and prepares it based on controller’s instructions
- View: Displays data to user in easy-to-understand format, based on the user’s actions
- Controller: Takes in user commands, sends commands to the model for data updates, sends instructions to view to update interface.
Or, in diagram form:
That was boring. Onto the bar.
A beginner web developer enters a bar…
You enter a bar on a Friday night, and approach the bartender. Since the bar is already crowded, you push through a crowd until you finally catch the bartender’s attention, and you blurt out, “One Manhattan, please!”
You are the user, and your drink order is the user request. To you, the Manhattan is just your favorite drink, and you pretty reliably know that this will be a sweet and delicious drink.
The bartender gives you a quick nod. To the bartender, the Manhattan is not a tasty drink, it is merely a series of steps:
- Grab glass
- Add whiskey
- Add vermouth
- Add bitters
- Stir drink
- Add cherry
- Ask for credit card and charge.
The bartender’s brain is the controller. As soon as you say the word “Manhattan” in a language that they understand, the work begins. This work is similar in nature to making a margarita or strawberry daiquiri, but uses distinct ingredients that will never be confused. The bartender can only use the tools and resources that are behind the bar. This limited tool set is the model, and includes the following:
- Bartender’s hands
- Shakers/mixing equipment
- Liquors
- Mixes
- Glasses
- Garnishes
Perhaps at a fancier bar, they might have a robot assistant! Or an automatic drink mixer. It does not matter to your particular bartender, who can only use the available resources.
Finally, the finished drink that you can see and consume is the view. The view is built out of the limited options from the model, and arranged and transmitted via the controller (that is, the bartender’s brain).