MVC 模式相对于老式的 3 层模式的主要优势是什么 [英] What are the main advantages of MVC pattern over the old fashioned 3-layer pattern

查看:14
本文介绍了MVC 模式相对于老式的 3 层模式的主要优势是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑在我的新项目中使用 MVC 模式,我可以清楚地看到能够将数据层(模型)稍微靠近表示层(视图)的主要优势,这将允许应用速度略有提高.但除了性能观点,MVC 与视图-逻辑-数据分层类型模式相比还有其他优势吗?

I am contemplating about using an MVC pattern in my new project and I can clearly see the main advantage of being able to put the data layer (the model) a little closer to the presentation layer (the view), which will allow a little increase in application speed. But apart from performance stand point are there any other advantages of MVC over the view-logic-data layered type pattern?

对于那些感兴趣的人,我刚刚上传了我创建的示例 PHP 代码,用于测试 MVC 的使用.我故意省略了所有的安全检查,使代码更容易阅读.请不要过多批评它,因为我知道它可以更加精致和先进,但是 - 它有效!!!我欢迎提出问题和建议:这里是链接:http://www.sourcecodester.com/sites/default/files/download/techexpert/test_mvc.zip

For those who's interested I just uploaded a sample PHP code that I created to test the use of MVC. I purposly omitted all the security checks to make the code a little easier to read. Please don't critisize it too much, because I know it could be a lot more refined and advanced, but nevertheless - it works!!! I will welcome questions and suggestions: Here is the link: http://www.sourcecodester.com/sites/default/files/download/techexpert/test_mvc.zip

推荐答案

被引用为 MVC 优势的关注点分离实际上也是 3 层/3 层系统的一个进步.在那里,业务逻辑也是独立的,可以从不同的表示层使用.

The separation of concerns that's quoted as being an advantage of MVC is actually also an advance of a 3-layer/3-tier system. There too, the business logic is independent and can be used from different presentation tiers.

主要区别在于,在经典 MVC 中,模型可以引用回视图.这意味着当数据更新时,模型可以将此数据推送回可能的多个视图.主要示例是桌面应用程序,其中数据以多种方式可视化.这可以像表格和图表一样简单.表中的更改(即一个视图中的更改)首先通过控制器推送到模型,然后将其推送回图形(另一个视图).然后图表会自行更新.

A main difference is that in classic MVC the model can have a reference back to the view. This means when data is updated the model can push this data back to possibly multiple views. The prime example is a desktop application where data is visualized in multiple ways. This can be as simple as a table and graph. A change in the table (which is a change in one view) is first pushed via the controller to the model, which then pushes it back to the graph (the other view). The graph then updates itself.

由于桌面开发正在走下坡路,许多程序员只在某些 Web 变体中接触 MVC,例如通过 Java EE 中的 JSF.

Since desktop development is on the decline, a lot of programmers have only come in touch with MVC in some web variant, e.g. via JSF in Java EE.

在这些情况下,模型几乎从不引用视图.这是因为 Web 主要是基于请求/响应的,并且在请求被提供之后,服务器不能发送额外的信息.IE.从模型推送到客户端的更新将毫无意义.使用反向 ajax/comet,这种情况正在发生变化,但许多基于 Web 的 MVC 框架仍然没有充分利用这一点.

In those cases the model almost never has a reference to the view. This is because the web is mainly request/response based and after a request has been served, the server cannot send additional information. I.e. an update pushed from the model to the client would be meaningless. With reverse ajax/comet this is changing, but many web based MVC frameworks still don't fully utilize this.

因此,在基于 Web 的 MVC 的情况下,M、V 和 C 之间的典型三角形"较少,并且该 MVC 变体实​​际上比真正的"MVC 更接近于 n 层模型.

Thus, in the case of web based MVC, the typical "triangle" between M, V and C is less there and that MVC variant is actually closer to an n-tier model than 'true' MVC is.

另请注意,某些 Web MVC 框架在 M、V 和 C 之间有一个中间管道部分,称为支持 bean (Java/JSF) 或代码隐藏 (ASP.NET).在 JSF 中,控制器由框架提供,视图通常不直接绑定到模型,而是使用此支持 bean 作为中介.支持 bean 非常小,基本上只是以一种方式从模型中预取数据,并将特定于模型的消息(例如异常)转换为特定于视图的消息(例如一些人类可读的文本).

Also note that some web MVC frameworks have an intermediate plumbing part between M, V and C called a backing bean (Java/JSF) or code behind (ASP.NET). In JSF the controller is provided by the framework, and the view often doesn't bind directly to the model but uses this backing bean as an intermediary. The backing bean is very slim and basically just pre-fetches data from the model one way and translates model specific messages (e.g. exceptions) into view specific messages (e.g. some human readable text).

这篇关于MVC 模式相对于老式的 3 层模式的主要优势是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆