MVC或MVP?哪种设计模式最有意义? [英] MVC or MVP? Which design pattern makes the most sense?

查看:163
本文介绍了MVC或MVP?哪种设计模式最有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们喜欢哪一个?我一直在研究这两者,似乎有人在他们称之为一些不一致。



我会尝试记下差异,你可以纠正如果我错了,我就是这样。



MVC


  1. 自己的观察者(Views),对通知观察员的模型的更新。

  2. 视图将所有事件和消息传递给Controller。当模型通知发生更改时,视图会更新其内容。视图保存对控制器和模型的引用。

  3. 控制器保存模型,(有时)持有视图。视图将调用与用户输入相对应的控制器方法,然后控制器会相应地调整模型,并有时会操作View(某些查看点击等阻止按钮)。

MVP


  1. 模型没有对View的引用。只提供程序的数据抽象。模型不会引用任何东西。

  2. 在MVC视图中,根据用户输入调用相应的Presenter方法。视图仅供参考者使用。

  3. 演示者有对视图和模型的引用。当一个视图在演示者中调用一个方法时,演示者操纵模型然后操纵视图。

我很确定我明白MVC如何工作,只是我的理解,如果MVP是一种iffy。我真的很喜欢MVC,但唯一不符合我的部分就是这样一个事实,即这个模型只能是数据层的抽象,同时也保留对Views的引用,并且更新。这是没有意义的。

解决方案

Martin Fowler提供了一个分析MVC和MVP,以及维基百科 MVP 文章给出更多参考。



对我来说,有两个问题:



1)。模型 - 视图关系如何活?如果模型动态更改,并且View(s)必须更新以反映该模型更改,那么我们有经典的MVC,并且模型以某种方式通知相关视图的更改。这种风格不适用于经典的Web Apps(例如在Struts中实现)。在这里,通常会有一个视图创建为模型的快照(实际上通常由模型提供的DTO)。在许多文献中,Web风格仍被称为MVC。



2)。当用户做某事时,谁负责求职和行事。在MVC中,这通常是控制器的工作。 MVP似乎允许从View到Model的更直接的交互(如果我正确地理解Fowlers文章)。



我更喜欢清晰的分离问题 - MVC方法是我的想法,但这可能只是一个熟悉的事情。



一个人应该选择什么?一般来说,我认为您是由您选择使用的框架驱动的。我来自Struts的背景,所以网络风格的MVC对我来说是很自然的。如果我明白,MVP在.NET的某些方面已经明确采用。我会随着你选择的任何框架的流动,我不会拒绝一个框架,因为它是MVP而不是MVC - 即使假设可以做出明确的区分。


Which do you guys prefer? I've been looking into both and there definitely seems to be some inconsistency in what people call them.

I will try and jot down what the differences are and you can correct me if I'm wrong.

MVC

  1. Model holds references to it's own observers (Views), on updates to the model it notifies observers.
  2. Views pass all events and messages to the Controller. Views update their content when they are notified by the model that a change has occured. View holds a reference to the Controller and to the Model.
  3. Controller holds the Model and (sometimes) the Views. The Views will call the Controllers methods corresponding with user input, the Controller then maniuplates the Model as accordingly, and sometimes manipulates the View (blocking out buttons on certain View clicks, etc. )

MVP

  1. Model has no references to the View. Only provides the data abstraction for the program. Model holds no reference to anything.
  2. As in MVC Views call the corresponding Presenter methods depending on user input. View has a reference only to the Presenter.
  3. Presenter has a reference to the Views and the Model. When a View calls a method in the Presenter, the Presenter manipulates the Model then manipulates the View.

I'm pretty sure I understand how MVC works, just my understanding if MVP is kind of iffy. I really really like MVC, but the only part that doesn't sit that well with me is the fact that the Model, which is supposed to only be an abstraction of the data layer, also holds references to Views and does updating. That doesn't make sense.

解决方案

Martin Fowler offers an analysis of MVC and MVP, and the Wikipedia MVP article gives more references.

For me, there are two questions:

1). How "live" is the Model-View relationship? If the Model changes dynamically, and the View(s) must update to reflect that Model change then we have classic MVC, and the Model somehow notifies the relevent Views of changes. This style doesn't apply to classic Web Apps (for example as implemented in Struts). Here there typically is a View created as a one off of a snapshot of the Model (indeed often on DTOs provided by the Model). In much literature the Web-style is still referred to as MVC.

2). When the user does "something", who is responsible for ineterpreting and acting. In MVC this is typically the Controllers job. MVP appears to allow for more direct interaction from View to Model for this purpose (if I understand Fowlers article correctly).

I much prefer clean separation of concerns - the MVC approach is how I think, but that may just be a familiarity thing.

What should a person pick? Generally, I think you are driven by the framework you choose to use. I come from a Struts background, so web-style MVC is natural for me. If I understand correctly MVP has explicit adoption in some aspects of .NET. I would go with the flow of whatever framework you choose, and I wouldn't reject a framework just because it is MVP rather than MVC - even assuming that a clear-cut distinction could be made.

这篇关于MVC或MVP?哪种设计模式最有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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