Java 中正确的 MVC 实现
我仍在尝试了解实现 MVC 的正确方法是什么。此示例 @oracle 表示视图可以访问控制器。另一个教程 @leepoint 指示视图有权访问模型。这些是 MVC 的不同变体吗?就我而言,我按照 Oracle 站点上的教程进行了一些修改(我在 AbstractController getModelProperty 中添加了一个函数,这将允许我检索当前注册模型的字段值,但我也可以通过传递模型作为参数(如 leepoint 教程中所示)来简化并可能优化视图的数据访问
。
I'm still trying to understand what is the correct way of implementing MVC. This example @oracle says that view has access to the controller. And another tutorial @leepoint is indicating that the view has access to model. Are these different variations of the the MVC? In my case I was following the tutorial at Oracle site with some modifications(I have added a function in AbstractController getModelProperty, which will allow me to retrieve the value of the fields of the current registered models, but it also could me sense to pass the model as parameter(like indicated at leepoint tutorial) to simplify and probably optimise the data access for the view.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
视图绑定到模型。由于视图渲染模型,因此它们必须对模型有深入的了解,因此根本没有办法绕过它。有些视图是通用的,并且这些视图具有“通用”模型。在这里,您可以尝试使您的实际模型符合通用模型,以便“通用”视图可以使用您的数据。但即使有了这些通用模型,视图仍然与它们紧密相连。
模型管理数据和状态。虽然视图对模型有深入的了解,但模型与视图无关。它根本不在乎。这样您就可以对同一模型有多个视图。
但是,模型必须将模型的更改告知其他人。通常在 java 中您使用 PropertyChangeListener。这种机制让模型可以大规模地喊出更改,任何感兴趣的人都可以监听这些更改并对其采取行动,例如您的观点。
一个简单的例子是,您的游戏对象可能会受到子弹的伤害,并且生命值会降低到 50% 以下。该视图可以看到生命值已减少并改变了模型的图像(例如添加烟雾或其他)。
控制器通常与视图和模型紧密绑定。它知道视图的功能(例如它的大小和其他感兴趣的区域),并且知道如何更改模型。例如,当单击鼠标时,控制器将鼠标点转换为相对于视图的坐标,并据此确定单击的对象。一旦确定了被单击的对象,它就可以将该对象的模型设置为“选定”。
然后该模型会广播它的“选定”属性已更改。视图看到这一点,找到发生更改的模型的边界矩形,并使该矩形在其显示上无效。
最后,Java 出现并告诉视图“嘿,矩形 10,10,100,100 需要绘制”。视图在该矩形中找到模型,用“选定的”边框或其他方式绘制对象的新视图。
这就是整个周期的运作方式。
Views are bound to models. Since views render models, they have to have intimate knowledge of the model, there's simply no way around it. Some views are generic, and these have "generic" models. Here, you may try and conform your actual model to the generic one so that the "generic" view can use your data. But even with these generic models, the views are still tightly bound to them.
Models manage data, the state. While a view has intimate knowledge of the model, the model is view agnostic. It simply doesn't care. This way you can have several views for the same model.
However, a model must inform others of changes to the model. Typically in java you use PropertyChangeListener's. This mechanism lets the model just shout out changes wholesale, and anyone interested can listen for these changes and act on them, for example your view.
A simple example is that you game object can take damage from a bullet, and it's reduced to below 50% health. The view can see that health has been reduced and change the image of the model (say adding smoke, or whatever).
The Controller typically is bound tightly to the view and the model. It knows the capabilities of the view (like it's size, and other areas of interest), and it knows how to change the model. Such as when mouse is clicked, the controller converts the mouse point in to a coordinate relative to the view, and from that determines what object was clicked. Once it determines the object that was clicked, it can set the model for the object to, say, "selected".
The model then broadcasts out that it's "selected" property has changed. The view sees this, finds the bounding rect for the model that changed, and invalidates that rectangle on its display.
Finally, Java comes around and tells the view "Hey, Rect 10,10,100,100 needs to be painted". And the view find the models in that rect, paints the new view of the object with a "selected" border, or whatever.
Thats how the whole cycle works.
两者都是。 MVC相互之间呈三角关系,Controller位于最上面。较新的方法是使用 MVP,其中 Presenter 位于模型和视图之间。
如果您可以将尽可能多的模型知识保留在视图之外,并且仅向其提供特定于其查看任务的信息,那就更好了。从长远来看,它确实会让你的生活更轻松。
It's both. MVC has a triangular relationship with each other, with the Controller on the top. The newer way to go is using MVP, where the Presenter sits between the model and the view.
It's better if you can keep as much of the model knowledge out of the view and only feed it information specific to it's viewing task. It does make your life easier in the long run.
是的,视图保存对用户手势控制器的引用。 (在 GUI 中,视图和控制器有时最终会混在一起)。
是的,视图通常包含对模型的引用(它可能有多个模型)。
有很多变化。
通常模型具有观察者视图,并且视图在收到更新消息时会自行更新,而不是直接调用由控制器。前者将视图与控制器更多地解耦。
yes, the view holds a reference to the controller for user gestures. (in a gui, the view and controller sometimes end up lumped together).
yes, the view usually holds a reference to the model(s) (it might have more than one).
there are a bunch of variations.
usually the model has views that are observers and the view updates itself when it receives an update message as opposed to being called directly by the controller. the former decouples the view more from the controller.
Swing 库是 MVC 模式的一个非常好的实现。只需研究一下 API,一切都会为您准备就绪。
The Swing libraries are a very good implementation of the MVC pattern. Just study the API for a bit and it will all fall into place for you.
关于 MVC 的维基百科文章很好地阐明了这些关系好吧:视图有模型,控制器有视图和模型,模型对视图和控制器一无所知。
在某些情况下,通过组合视图和控制器来简化模式可能是有利的。
The Wikipedia Article on MVC nails the relationships pretty well: View has the Model, Controller has both the View and the Model, Model knows nothing about both View and Controller.
In some cases it may be advantageous to simplify the pattern by combining View and Controller.