Model-View-Presenter中的Presenter是否创建视图? [英] Does Presenter in Model-View-Presenter create views?

查看:134
本文介绍了Model-View-Presenter中的Presenter是否创建视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MVP中创建视图?演示者是否总是创建它们(除了在子视图中使用View之外)?还是它是一个单独的第三方组件或App或创建它们的东西?

How are Views created in MVP? Does the Presenter always create them (in addition to View in case of subviews)? Or is it a separate third-party component or App or something that creates them?

我们还要补充一点,我可能打算在Dojo Toolkit/ExtJS(即JavaScript)上进行此操作.

Let's also add that I'm probably going to do this on Dojo Toolkit/ExtJS (JavaScript that is).

所以,我有以下代码行:

So, I have these code lines:

var v = new MyApp.view.User();
var p = new MyApp.presenter.User();

这两行应该准确地放在哪里?主持人是否实例化视图,反之亦然?实例化第一个实例的是什么?

where should both lines go exactly? Does the presenter instantiate the view, or vice-versa? And what instantiates the first instance?

推荐答案

这取决于...

MVP的主要目标是将复杂的决策逻辑与UI代码分开,从而使两者都变得更易于理解和维护.通常,另一个目标是使演示者中的决策逻辑可测试.

The main goal of MVP is to separate complex decision logic from UI code in such a way that both become easier to understand and to maintain. Often another goal is to make the decision logic in the presenter testable.

FVP在2004年描述了MVP模式,他在2006年退休,模式转换为监督控制器(SC)和

The MVP pattern was described by Fowler in 2004 and he retired it in 2006 by splitting the pattern into Supervising Conroller (SC) and Passive View (PV). In SC, the View is bound to the Model but not in PV; in PV, the View is only changed by the Presenter directly.

在SC和PV中,演示者都必须更新视图对用户对视图所做的更改(例如输入文本或按下按钮)做出反应.当您在Presenter上使用View调用方法时,就会出现您描述的问题,因为View需要对Presenter的引用,反之亦然.如果您这样做,您只需做出决定,由谁来启动.选项是:

In both SC and PV, the Presenter has to update the View and react to changes the user made to the View, such as entering text or pressing a button. When you let the View call methods on the Presenter, then the problem you describe arises because the View needs a reference to the Presenter and vice versa. If you do this, you simply can make a decision who starts it all up. Options are:

  1. 视图创建Presenter的实例.加载视图后,它会在Presenter上的初始化函数中将自身传递给Presenter.
  2. 另一种方式:Presenter创建View并将其通过View的初始化函数传递给View.
  3. 您将引入第三个对象,该对象同时创建View和Presenter,并将它们连接在一起并对其进行初始化.

所有选项都使您可以实现关注点分离和增强决策逻辑的可测试性的"MVP目标".我不认为这些方法在理论上是对还是错–您只需要选择最适合您所使用技术的方法即可.最好在整个应用程序中保持一致.

All options allow you to reach "MVP goals" of separation of concerns and increased testability of decision logic. I don't think any of these methods is theoretically right or wrong –you just have to pick the one that is most appropriate to the technology you use. And it's best to be consistent in your choice throughout the application.

这篇关于Model-View-Presenter中的Presenter是否创建视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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