We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
几乎每个框架的 MVC 方式都不同,因此您最终可能会更加困惑。 MVC 的一般原则非常简单:“模型是状态;视图对模型做出反应;控制器对视图作出反应;控制器更改模型”。 模型、视图和控制器都是概念——它们就是你感觉的任何东西。 类、类群、带有 XML 配置文件的类实例,应有尽有。
我实际上认为这涵盖了基本原则。 如果没有框架,你就不会走得更远。 重要的是特定框架如何定义模型、视图和控制器以及它们的交互。
Almost every framework does MVC differently, so you might end up getting even more confused. The general principles of MVC are very simple: "Model is state; view reacts to model; controller reacts to view; controller changes model". The model, view and controller are concepts - they are whatever you feel them to be. Classes, bunches of classes, instances of classes with XML configuration files, you name it.
I actually think that about covers the basic principles. Without a framework, you'd not get much further. What matters is how a particular framework defines model, view and controller and their interactions.
MVC 基本上只是将代码拆分为处理数据的模型、显示数据的视图以及将数据从模型传递到视图的控制器。
您不需要 API 或框架,它只是一种拆分代码的方法。 许多框架使用它的原因是因为它是一个非常简单的概念,它适用于很多事情(它完美地适合网页),并且相当灵活(例如,使用 Rails,您可以做所有事情)你的视图,或模型/控制器,如果你需要的话..)
Python 中的一个快速示例,示例 MVC 结构化 Python 脚本。 不一定是“最佳实践”,但它有效,而且相当简单:
MVC is basically just splitting up your code into a Model, which deals with the data, a View which displays the data, and a Controller which passes data from the Model to the View.
It's nothing you need an API or framework for, it's just a way of splitting up your code. The reason many frameworks use it is because it's a very simple concept, it works well for many things (it fits webpages perfectly), and is fairly flexible (for example, with Rails, you could do everything in your view, or model/controller, if you so-desired..)
A quick example in python, of an example MVC structured Python script. Not necessarily "best practices", but it works, and is fairly simple:
除了 Sander 的回复之外,我想说大多数框架都混淆了前端控制器和MVC。 它们实际上是两个完全独立的概念,但它们通常都存在于框架中。 所以要小心。
In addition to Sander's reply, I'd say that most frameworks confuse front controller and MVC. They are really two completely separate concepts, but they are often both present in frameworks. So watch out for that.
MVC 的主要优点是关注点分离。 当你编写代码时,如果你不小心,它可能会变得一团糟。 因此,从长远来看,了解如何将模型、视图和控制器放在不同的“孤岛”中可以节省您的时间。 任何策略都是好的。
所以这是我的:
The main advantage of MVC is separation of concerns. When you write code, and if you're not careful, it can become a big mess. So knowing how to put Models, Views, and Controllers in different "silos" saves you time in the long term. Any strategy is good.
So here is mine :
我知道现在已经晚了,但我相信稍后人们会提出同样的问题。
我认为上面非常好的代码示例最好像这样放置,但是 YMMV:
这是另一个使用继承的示例,它在 python/php 中非常有用......
如果这有意义,请转到 django,现在您准备好了。 只要阅读免费书籍(如果这有意义),您就会快速浏览它。 你的权利,但在使用 django 之前你必须能够了解 OOP 和 MVC 范例,因为它是通过这些范例构建和使用的。
正如您所看到的,它并不复杂,它只是保持代码有序的众多方法之一。
这解释了 django 中的 MVC
Know it is late but I am sure people will come along later with the same question.
I think the very good code example above is better put like this but YMMV:
Here is another example using inheritance which can be very useful in python/php.....
If this makes sense go to django now your ready. Just read the free book if this makes sense you will go through it rapidly. Your right though you must be able to know about OOP and MVC paradigms before using django as it is built and used via these paradigms.
As you see it is not complex it is just one of the many ways to keep your code in order.
This explains MVC in django