基本 MVC 模式通信

发布于 2024-12-10 08:09:34 字数 820 浏览 0 评论 0原文

我刚刚开始研究模型视图控制器模式。我现在了解了MVC的基本用法,但是当我尝试在一个简单的测试中实现MVC时,我遇到了一个问题。当然,我可以轻松调整代码以使其正常工作,但我希望学习如何正确实现 MVC 模式。

测试: 我使用actionscript 3 制作了一个简单的程序。它由一个炮塔和一个鼠标点击组成。炮塔位于屏幕中间。当我单击任意位置时,炮塔会向我单击的位置旋转。鼠标和炮塔都有自己的模型、视图和控制器。当我单击时,MouseModel 会正确更改。但要使实际的 TurretView 做出响应,TurretModel 必须更改其旋转变量并发出事件。

问题是谁响应 MouseModel 事件?

     /------->MouseControl------\
    /                            \
MouseView                ?<---MouseModel

TurretView <------------------TurretModel

             TurretControl

我认为最好不要让 MouseModel 直接影响 TurretModel 或 TurretControl,因为这需要它们成为事件监听器。让TurretView监听MouseModel,然后告诉TurretControl调整TurretModel,之后TurretView可以通过TurretModel事件进行更新,对于一个简单的任务来说,看起来像是很多额外的代码。另外,我不想让 MouseControl 影响 TurretModel,这会破坏鼠标作为未来类输入的灵活性。

哦,我还要将角度计算的代码放在哪个类中?

提前致谢

I just started to study the Model View Controller pattern. I now understand the basic usage of MVC, but when I tried to implement MVC in a simple test, I ran into a problem. Of course I could easily adjust the code so it works, but I wish to learn how to correctly implement the MVC pattern.

The test:
I used actionscript 3 to make a simple program. It consist of a turret, and a mouseclick. The turret is in the middle of the screen. When I click anywhere the turret rotates towards the point where I clicked. Both the mouse and the turret have their own model,view and controller. When I click, the MouseModel changes correctly. But for the actual TurretView to respond, the TurretModel must change its rotation variable and send out an event.

The question is who responds to the MouseModel event?

     /------->MouseControl------\
    /                            \
MouseView                ?<---MouseModel

TurretView <------------------TurretModel

             TurretControl

I figured its best not to have MouseModel directly influence TurretModel or TurretControl, because this would require them to be an eventListener. Making TurretView listen to the MouseModel, and then tell TurretControl to adjust TurretModel, after wich TurretView can update through a TurretModel event looks like a lot of extra code for a simple task. Also I'd rather not have MouseControl affect TurretModel, this would break the flexibility of the mouse as input for future classes.

Ow, also in which class do I put the code for the angle calculation?

Thanks in advance

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

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

发布评论

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

评论(2

绮筵 2024-12-17 08:09:34

请记住,MVC 的目标主要是模型和视图的分离,控制器可以充当两者之间的通信者。

除非您计划存储发生的每个操作(单击、旋转等),否则实际上不需要操作(在这种情况下)将数据发送到模型。您想做的一切都应该可以通过控制器轻松处理。所以流程是:

  • 鼠标单击
  • 事件被触发以触发命令(在控制器中),传递鼠标位置
  • 该命令计算炮塔的旋转
  • 该命令告诉视图旋转炮塔

这当然是我的建议脱离你的例子。事实上,根据项目的不同,上述流程很容易改变(例如,在这种情况下,在命令中进行旋转计算似乎很好,但在其他情况下可能没有多大意义)。将 MVC 视为一个目标 - 您试图尽可能地分离这些元素,但没有 100%“每次都有效”的方法来做到这一点。

Robotlegs 是一种流行的 MVC 框架,在其网站上有一个很棒的图表,展示了他们如何处理 MVC:

http://www.robotlegs.org/diagram/。 Robotlegs.org/diagram/

我并不是在宣传您需要使用 Robotlegs(它很好,但还有很多其他选择),但他们确实制作了一个漂亮的图表:)

Remember that the goal with MVC is primarily the separation of Model and View, and the Controller can serve as the communicator between the two.

Unless you are planning on storing each action that occurs (clicking, rotating, etc.), there is no real need for an action (in this situation) to send data to the Model. Everything you'd like to do should be easily handled with the Controller. So the flow would be:

  • Mouse click
  • Event is fired to trigger a command (in the Controller), passing along the mouse location
  • The command calculates the turret's rotation
  • The command tells the View to rotate the turret

This is, of course, my suggestion based off of your example. In truth, depending on the project, the above flow could easily change (for instance, in this situation it seems good to do rotation calculation in the command, but in other situations that may not make as much sense). Look at MVC as a goal - you're trying to separate these elements as much as possible, but there is no 100% "works-every-time" way to do it.

Robotlegs, a popular MVC framework, has a great diagram on their site showing how they tackled MVC:

http://www.robotlegs.org/diagram/

I'm not advertising that you NEED to use Robotlegs (it's good, but there's plenty of other alternatives), but they definitely made a pretty diagram :)

妖妓 2024-12-17 08:09:34

您应该有一个模型,其中包含您当前称为 MouseModel 和 TurretModel 的部件。您可以保留这些名称和细分,或者一旦您对需要完成的事情有了更好的“处理”,就可以做其他事情。

跟踪鼠标点击的视图应该调度一个事件,您的控制器组件捕获该事件来更新 TurretModel(此时,可能不需要 MouseModel)。然后,您的 TurretView 可以根据 TurretModel 更新自身,或者控制器可以根据新值更新 TurretView。这将取决于您如何接线。

使用像 Robotlegs 这样的框架可以帮助你弄清楚这个过程的所有来龙去脉,我听说这本书 http://shop.oreilly.com/product/0636920021216.do 提供了 MVC 的精彩解释,即使您不了解阅读后选择使用 Robotlegs。

You should have one model, which then has pieces in it that you are currently calling MouseModel and TurretModel. You can either keep those names and breakdowns, or do something else once you have a better "handle" on what needs to be done.

The View that is tracking the mouse clicks should dispatch an event that your Controller component catches to update the TurretModel (at this point, there's probably no need for a MouseModel). Your TurretView can then update itself based on the TurretModel, or the Controller can update the TurretView based on the new value. This will depend on how you have it wired.

Usin a Framework such as Robotlegs can help you figure out all the ins and outs of this process, and I've heard that this book http://shop.oreilly.com/product/0636920021216.do provides a great explanation of MVC, even if you don't choose to use Robotlegs after you read it.

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