我无法理解 MVC,它是什么,不是什么?

发布于 2024-07-16 06:17:08 字数 865 浏览 7 评论 0原文

我无法理解 MVC 是什么,我应该获得什么心态或编程模型,以便 MVC 的东西可以立即“灯泡”在我的头上? 如果不是立即,我应该首先尝试执行哪些简单的程序/项目,以便我可以应用 MVC 为编程带来的巧妙功能。

OOP 直观且更容易,对象就在我们身边,使用 OOP 范式进行代码重用的好处立即惠及任何人。 你可能可以在几分钟内与任何人谈论 OOP 并讲授一些示例,他们就会明白。 虽然 OOP 在某种程度上提高了编程的直观性,但 MVC 似乎反其道而行之。 我有一些消极的想法,认为一些未来的雇主(甚至客户)会因为我不使用 MVC 技术而看不起我。

虽然我可能了解 MVC 的 skinnable 方面,但是当我尝试应用时到我自己的项目,我不知道从哪里开始。

而且一些程序员甚至对于如何正确地完成MVC存在着不同的看法。

以 Jeff 关于 MVC 的帖子为例:

视图就是你如何放置 数据输出,如何显示。 如果你 想要一些数据的子集,对于 例如,我的观点是 模型的责任。

因此,也许有些程序员使用 MVC,但他们不知何故无意中使用视图或控制器来提取数据的子集。

为什么我们无法明确定义什么以及如何正确实现 MVC?

而且,当我搜索 MVC .NET 程序时,大多数都适用于 Web 程序,而不是桌面应用程序,这进一步引起了我的兴趣。 我的猜测是,这对网络应用程序来说是最有利的,桌面应用程序中混合视图(html)和控制器(程序代码)没有太大问题。

I cannot grok what MVC is, what mindset or programming model should I acquire so MVC stuff can instantly "lightbulb" on my head? If not instantly, what simple programs/projects should I try to do first so I can apply the neat things MVC brings to programming.

OOP is intuitive and easier, object is all around us, and the benefits of code reuse using OOP-paradigm instantly click to anyone. You can probably talk to anybody about OOP in a few minutes and lecture some examples and they would get it. While OOP somehow raise the intuitiveness aspect of programming, MVC seems to do the opposite. I'm getting negative thoughts that some future employers(or even clients) would look down upon me for not using MVC technology.

Though I probably get the skinnable aspect of MVC, but when I try to apply it to my own project, I don't know where to start.

And also some programmers even have diverging views on how to accomplish MVC properly.

Take this for instance from Jeff's post about MVC:

The view is simply how you lay the
data out, how it is displayed. If you
want a subset of some data, for
example, my opinion is that is a
responsibility of the model.

So maybe some programmers use MVC, but they somehow inadvertently use the View or the Controller to extract a subset of data.

Why we can't have a definitive definition of what and how to accomplish MVC properly?

And also, when I search for MVC .NET programs, most of it applies to web programs, not desktop apps, this intrigue me further. My guess is, this is most advantageous to web apps, there's not much problem about intermixed view(html) and controller(program code) in desktop apps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

骄傲 2024-07-23 06:17:08

我喜欢 Martin Fowler 的说法:)

http://martinfowler.com/eaaCatalog/modelViewController.html< /a>

.. 以及来自 http://martinfowler.com/eaaDev/uiArchs.html

以模型-视图-控制器为例。 它通常被称为模式,但我发现将其视为模式并不是很有用,因为它包含很多不同的想法。 不同的人在不同的地方阅读有关 MVC 的内容,从中获得不同的想法,并将它们描述为“MVC”。 如果这还没有引起足够的混乱,那么您就会受到通过中国耳语系统发展起来的对 MVC 的误解的影响。

I like the way Martin Fowler puts it :)

http://martinfowler.com/eaaCatalog/modelViewController.html

.. and from http://martinfowler.com/eaaDev/uiArchs.html :

Take Model-View-Controller as an example. It's often referred to as a pattern, but I don't find it terribly useful to think of it as a pattern because it contains quite a few different ideas. Different people reading about MVC in different places take different ideas from it and describe these as 'MVC'. If this doesn't cause enough confusion you then get the effect of misunderstandings of MVC that develop through a system of Chinese whispers.

Oo萌小芽oO 2024-07-23 06:17:08

您可以首先阅读此问题的答案。

You could start by reading the answers to this question.

疯狂的代价 2024-07-23 06:17:08

MVC 确实非常适合 WebApps。 大多数WebApps从数据库获取数据,稍微处理一下然后将它们显示给用户。 DB 是模型级别,控制器进行处理,视图仅发出 HTML 代码。

有些人认为控制器应该只从模型获取数据并将其提供给视图,但我认为这意味着控制器没有用,因此它只是 MV 编程模型:)
但我理解他们,如果您使用某种处理,您将多次使用它,因此最好在模型级别执行此操作,它可以通过这种方式在应用程序的不同部分之间共享。

您不必对桌面应用程序使用 MVC,事实上,我不确定此模型是否适用于大型应用程序,而桌面应用程序通常是这样。 您经常想要使用“组件”,即可以在应用程序的不同部分提供相同的数据并从中获取相同的 UI,而这只会导致使用 M​​VC 复制代码。

MVC does fit WebApps nicely. Most of WebApps get the data from DB, process it just a little and show them to the user. DB is your model level, controller does processing and view just emits HTML code.

Some people thinks controllers should just get the data from model and feed it to the view but I think this means the controllers are useless and thus it is just MV programming model :)
But I understand them, if you use some kind of processing, you're going to use it more than once and thus it is better to do that at the model level, it can be shared between different parts of applications this way.

You don't have to use MVC for Desktop apps, in fact, I'm not sure this model will work for huge apps, which desktops ones usually are. Too often you want to use a "component", thing that you can feed the same data and get the same UI out of it but in different parts of app, and it just leads to copying code around with MVC.

霓裳挽歌倾城醉 2024-07-23 06:17:08

我发现 MVC 范例常常过于臃肿。 简单的模型/视图(没有控制器)更容易理解并且更容易实现。

大多数人都知道某个类保存数据(模型)和加载/保存数据的逻辑,另一个类显示该数据(视图)。
给它一些胶水来加载文档(类似于文档管理器),然后就完成了。

I found the MVC paradigm often a lot too bloated. A simple model/view (without the controller) is easier to understand and easier to implement.

Most people understand that some class holds the data (model) and the logic to load/save the data and another class shows that data (view).
Give it some glue to load a document (something like a document manager) and you are done.

相权↑美人 2024-07-23 06:17:08

“典型的”MVC 实现将具有:

  • 视图 - 呈现数据以供最终用户查看,并为他们提供用户界面对象以与数据交互(但不处理该交互)
  • 控制器 - 处理用户交互和操作视图和模型之间传输的数据的
  • 模型 - 处理数据的存储以及应用程序的数据表示

MVC 模式的频繁使用是 Web 应用程序的视图部分的 HTML/CSS/浏览器渲染, PHP/脚本语言应用程序级别充当控制器,MySQL 或类似的数据库充当模型(前面可能有也可能没有某种 ORM 框架)。

事实是,很少有人以这种格式使用 MVC,并且有充分的理由(在我看来)。 不同的应用有不同的需求和设计要求,根据需要调整模式是有益的。 如今,“MVC”似乎更多地描述了隔离应用程序各层的框架的用法,而不是特定的模型-视图-控制器模式。

从雇主的角度来看,真正需要的(通常)是抽象层以及来自构建 n 层应用程序的经验。 当你不再使用一个应用程序时,会发生相当大的心理跳跃,该应用程序不仅可以分为垂直功能,还可以分为水平功能层。

需要注意的是,换肤实际上只是一个特定层的一个方面,您可以更改模型层的数据源、控制器层操作该数据的方法(例如新算法),并逐渐推出它们,而无需更改其他层。

An "typical" MVC implementation would have:

  • views - renders the data for the end user to see and gives them user interface objects to interact with the data (but not to handle that interaction)
  • controller(s) - handles the user interaction and manipulation of data traveling between the views and the model
  • model - handles storage of data and the representation of that data for the application

A frequent usage of the MVC pattern would be the HTML/CSS/browser rendering for the view portion of a web application, the PHP/scripting language application level acting as the controller, and MySQL or a similar database acting as the model (which may or may not have some sort of ORM framework in front of it).

The truth is, very few people use MVC in exactly this format, and for good reason (in my opinion). Different applications have different needs and design requirements and it's beneficial to bend the patterns to what is needed. These days, "MVC" seems to describe more of the usage of a framework that isolates the layers of an application than the specific model-view-controller pattern.

From an employer perspective, what is really being looked for (oftentimes) is that layer of abstraction and the experience that comes from building an n-layered application. There's a fairly sizable mental jump that happens when you move from working in an app that can be divided up not only into vertical features but into horizontal layers of features as well.

As a note, skinning is really just an aspect of one particular layer, you can change the data sources at the model layer, the methods of manipulating that data (a new algorithm for instance) at the controller layer and roll them out gradually without having to change the other layers.

拔了角的鹿 2024-07-23 06:17:08

也许遵循维基百科文章可以推荐你到更多的实施。 它包含 MVC 作为 GUI 框架的实现的链接

Maybe following the Wikipedia Article can refer you to more implementations. It has links to implementations of MVC as GUI frameworks.

久而酒知 2024-07-23 06:17:08

MVC 是一种模式,它与 OOP 配合得很好,两者并不排斥——我想说它们是正交的。 MVC 模式试图将显示代码 (V) 与数据 (M) 和控制流 (C) 分开。

如果你搜索“MVC .Net”,你几乎肯定会得到很多关于 Web 应用程序的搜索结果,因为 ASP.Net MVC 框架最近发布了,这是 MVC 模式在 ASP.Net 编程中的应用,因此它是用于开发网站。 正如您所建议的,没有理由不将该模式也应用于桌面应用程序。

如果您在 MVC 方面遇到困难,我建议您阅读一般模式,以了解人们如何使用模式谈论建模问题/解决方案,然后更详细地了解 MVC。

编辑 参考 Fowler 的书企业应用程序架构模式 e 是阅读 MVC 和其他模式的绝佳场所。

MVC is a pattern, and it works well with OOP, the two are not exclusive - I'd say they were orthogonal. The MVC pattern attempts to separate the display code (V) from the data (M) and the flow of control (C).

If you search for "MVC .Net" you will almost certainly get a lot of hits for web applications because the ASP.Net MVC framework was recently released, and this is an application of the MVC pattern to ASP.Net programming, hence it is used for developing websites. As you suggest there is no reason not to apply the pattern to desktop apps too.

If you are having a hard time with MVC I would recommend reading up patterns in general to get a feel for how people talk about modeling problems/solutions using patterns, then look at MVC in more detail.

Edit Prompted by the reference to Fowler, his book Patterns of Enterprise Application Architecture is an excellent place to read up on MVC and other patterns.

随遇而安 2024-07-23 06:17:08

MVC 范例是将应用程序分为三个部分的方法:模型、视图和控制器。 MVC 最初是为了将传统的输入、处理、输出角色映射到GUI领域而开发的。

用户输入、外部世界的建模以及对用户的视觉反馈由模型、视口和控制器对象分离和处理。 控制器解释来自用户的鼠标和键盘输入,并将这些用户操作映射为发送到模型和/或视口的命令,以实现适当的更改。 该模型管理一个或多个数据元素,响应有关其状态的查询,并响应更改状态的指令。 视口管理显示的矩形区域,并负责通过图形和文本的组合向用户呈现数据... 了解更多 & 甚至更多

The MVC paradigm is a way of breaking an application into three parts: the Model, the View and the Controller. MVC was originally developed to map the traditional input, processing, output roles into GUI realm.

The user input, the modeling of the external world, and the visual feedback to the user are separated and handled by model, viewport and controller objects. The controller interprets mouse and keyboard inputs from the user and maps these user actions into commands that are sent to the model and/or viewport to effect the appropriate change. The model manages one or more data elements, responds to queries about its state, and responds to instructions to change state. The viewport manages a rectangular area of the display and is responsible for presenting data to the user through a combination of graphics and text... read more & even more.

瞳孔里扚悲伤 2024-07-23 06:17:08

从本质上讲,MVC 只是关注点分离的一种特定情况:一块代码负责存储数据,第二块代码负责操作数据,第三块代码负责与用户交互。

这些块中的任何一个都可以以 OO 方式实现,也可以不以 OO 方式实现 - MVC 和 OOP 是正交的。 (尽管根据我的经验,MVC 应用程序和框架极有可能是面向对象的。)

At its core, MVC is just a specific case of separation of concerns: One chunk of code is responsible for storing the data, a second chunk is responsible for manipulating the data, and a third takes care of interacting with the user.

Any of these chunks can be implemented in an OO manner, or not - MVC and OOP are orthogonal. (Although, in my experience, MVC applications and frameworks are extremely likely to be OO.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文