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

本文是小编为大家收集整理的关于与老式的3层模式相比,MVC模式的主要优势是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在考虑在我的新项目中使用MVC模式,我可以清楚地看到能够将数据层(模型)放置在更接近演示层(视图)的主要优点,这将允许应用速度有所提高.但是,除了性能方面,MVC比视图data分层型模式还有其他优势吗?

编辑: 对于那些感兴趣的人,我刚刚上传了我为测试MVC使用的样本PHP代码.我故意省略了所有安全检查,以使代码更易于阅读.请不要使它变性太多,因为我知道它可能会更加精致和先进,但仍然可以使用!!!我欢迎您的问题和建议:这是链接: http:/http:/http:/http://www.sourcecodester.com/sites/default/files/download/techexpert/test_mvc.zip

推荐答案

所引用的担忧的分离实际上也是3层/3层系统的进步.在那里,业务逻辑是独立的,可以从不同的演示层使用.

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

由于桌面开发正在下降,因此许多程序员在某些Web版本中仅与MVC保持联系,例如通过JSF在Java Ee中.

在那些情况下,模型几乎从未参考过观点.这是因为Web主要基于请求/响应,并且在向请求提供后,服务器无法发送其他信息. IE.从模型推向客户端的更新将是毫无意义的.使用反向AJAX/COMET,这正在发生变化,但是许多基于Web的MVC框架仍然无法完全利用此功能.

因此,在基于Web的MVC的情况下,M,V和C之间的典型"三角"在那里较少,并且MVC变体实际上更接近N层模型,而不是'true'MVC.

还要注意,某些Web MVC框架在M,V和C之间具有中间管道零件,称为Backing Bean(Java/jsf)或后面的代码(ASP.NET).在JSF中,控制器是由框架提供的,并且该视图通常不会直接与模型结合,而是将此备用BEAN用作中介.背bean非常苗条,基本上只是将模型中的数据预先提取,并将模型特定消息(例如异常)转换为视图特定消息(例如,某些人类可读文本).

其他推荐答案

旁边

  • 代码重复使用,
  • 分开关注,
  • 较少的层之间的耦合,

@bakoyaro和@arjan已经提到了

我认为,与"构型构型"模式结合使用时,MVC比3层好. (即" Ruby on Rails"或Microsofts" ASP.NET的MVC").

我认为,这种组合导致更好,更轻松的代码维护.

首先,它使学习MVC-Framework的学习更加困难,因为您必须学习约定(LA控制器进入控制器文件夹,并且必须命名为XXXXXXController)

但是,在您了解了惯例之后,维护自己和外国代码更容易.

其他推荐答案

忘记通过移动到MVC来提高应用程序速度.我发现最大的好处可以轻松重复使用.一旦移至MVC,就不会依赖数据或实际数据存储.

例如,您可以有一天写的一个servlet作为您的演示层,第二天将Web服务写为现有模型和控制器的另一个演示层.如果您想或需要切换DBM,请像明智的一样.由于访问模型与其他所有内容完全分开,因此您只需要重写数据访问对象即可以您的控制器可以处理的方式返回数据.

通过将关注点分为3个不同的部分,您还可以促进真正的单位测试.可以免费测试您的演示层,并反之亦然.

在旁注上,我经常觉得MVC缩写不准确.每当我看到它时,我都会将其视为 view->控制器 - >模型.演示层永远不会有DAO代码,并且该模型将永远不会具有演示逻辑.控制器被迫充当介于中间的范围.

本文地址:https://www.itbaoku.cn/post/627665.html

问题描述

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?

EDIT: 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

推荐答案

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.

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.

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.

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.

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.

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).

其他推荐答案

Beside

  • code reuse,
  • separating of concerns,
  • less coupling between the layers,

already mentioned by @bakoyaro and @arjan

i think that MVC is better than 3-tier when combined with the "convention over configuration" pattern. (i.e. "ruby on rails" or Microsofts "MVC for asp.net").

In my opinion this combination leads to to better and easier code maintanance.

In the first place it makes learning the mvc-framework a bit more difficuilt since you have to learn the conventions (a la controllers go into the controllers folder and must be named xxxxxcontroller)

But after you learned the conventions it is easier to maintain your own and foreign code.

其他推荐答案

Forget increasing application speed by moving to MVC. I have found the biggest benefit to be ease of code reuse. Once you move to MVC, there are no dependencies on the presentation of your data or the storage of the actual data.

For example you could write a servlet that served up .jsp pages as your presentation layer one day, and the next day write a web service as another presentation layer to your existing Model and Controller. Like wise if you want or need to switch your DBMS. Since accessing the Model is completely separate from everything else, you would just need to re-write just your data access objects to return the data in a way your Controller can handle it.

By separating concerns into 3 distinct pieces, you also facilitate true unit testing. Your Presentation layer can be tested free of the Model or Controller, and vice-a-versa.

On a side note, I've often felt that the MVC abbreviation was inaccurate. Whenever I see it I think of it as View->Controller->Model. The presentation layer will never have DAO code in it, and the model will never have presentation logic in it. The Controller is forced to act as a go-between.