MVC 模式和 Swing [英] The MVC pattern and Swing

查看:20
本文介绍了MVC 模式和 Swing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在真实的 Swing 生活"中最难真正掌握的设计模式之一是 MVC 模式.我已经阅读了该站点上很多讨论该模式的帖子,但我仍然觉得我对如何在我的 Java Swing 应用程序中利用该模式没有清楚的了解.

One of the design patterns which I find most difficult to get a real grasp of in "real Swing life" is the MVC pattern. I've been through quite a few of the posts at this site which discuss the pattern, but I still do not feel that I have a clear understanding of how to take advantage of the pattern in my Java Swing application.

假设我有一个 JFrame,其中包含一个表格、几个文本字段和几个按钮.我可能会使用 TableModel 来桥接"JTable 与底层数据模型.但是,所有负责清除字段、验证字段、锁定字段以及按钮操作的函数通常会直接进入 JFrame.但是,这不是混合了模式的控制器和视图吗?

Let's say that I have a JFrame which contains a table, a couple of text fields and a few buttons. I would probably use a TableModel to "bridge" the JTable with an underlying data model. However, all functions responsible for clearing fields, validating fields, locking fields along with button actions would usually go directly in the JFrame. However, doesn't that mix the Controller and View of the pattern?

据我所知,在查看 JTable(和模型)时,我设法正确"实现了 MVC 模式,但是当我将整个 JFrame 视为一个整体时,事情变得混乱.

As far as I can see, I manage to get the MVC pattern "correctly" implemented when looking at the JTable (and the model), but things get muddy when I look at the entire JFrame as a whole.

我真的很想听听其他人对此的看法.当您需要使用 MVC 模式向用户显示一个表格、几个字段和一些按钮时,您会怎么做?

I'd really like to hear how others go about with regard to this. How do you go about when you need to display a table, a couple of fields and some buttons to a user using the MVC pattern?

推荐答案

我强烈推荐给你的一本关于 Swing MVC 的书是 Freeman 和 Freeman 的Head First Design Patterns".他们对 MVC 有非常全面的解释.

A book I'd highly recommend to you for MVC in swing would be "Head First Design Patterns" by Freeman and Freeman. They have a highly comprehensive explanation of MVC.

简要总结

  1. 您是用户——您与视图进行交互.视图是模型的窗口.当您对视图执行某些操作时(例如单击播放按钮)然后视图告诉控制器你做了什么.这是控制器的工作来处理这个.

  1. You're the user--you interact with the view. The view is your window to the model. When you do something to the view (like click the Play button) then the view tells the controller what you did. It's the controller's job to handle that.

控制器要求模型改变它的状态.控制器采取你的行动并解释它们.如果你点击一个按钮,控制器的工作是弄清楚这意味着什么并且应如何根据该操作操作模型.

The controller asks the model to change its state. The controller takes your actions and interprets them. If you click on a button, it's the controller's job to figure out what that means and how the model should be manipulated based on that action.

控制器也可能要求视图改变.当控制器从视图接收到一个动作时,它可能需要告诉视图视图因此而改变.例如,控制器可以启用或禁用界面中的某些按钮或菜单项.

The controller may also ask the view to change. When the controller receives an action from the view, it may need to tell the view to change as a result. For example, the controller could enable or disable certain buttons or menu items in the interface.

模型在其状态发生变化时通知视图.当模型中发生某些变化时,基于您采取的某些操作(例如单击按钮)或其他一些内部更改(例如下一个播放列表中的歌曲已经开始),模型通知视图它的状态发生了变化.

The model notifies the view when its state has changed. When something changes in the model, based either on some action you took (like clicking a button) or some other internal change (like the next song in the playlist has started), the model notifies the view that its state has changed.

视图向模型询问状态.视图直接从模型中获取它显示的状态.例如,当模型通知视图有新歌开始播放,视图从模型请求歌曲名称并显示它.该视图可能还向模型询问状态作为控制器的结果请求对视图进行一些更改.

The view asks the model for state. The view gets the state it displays directly from the model. For instance, when the model notifies the view that a new song has started playing, the view requests the song name from the model and displays it. The view might also ask the model for state as the result of the controller requesting some change in the view.

来源(如果您想知道什么是奶油控制器"是,想想奥利奥饼干,控制器是奶油中心,视图是顶部饼干,模型是底部饼干.)

Source (In case you're wondering what a "creamy controller" is, think of an Oreo cookie, with the controller being the creamy center, the view being the top biscuit and the model being the bottom biscuit.)

嗯,如果您有兴趣,可以从 这里

Um, in case you're interested, you could download a fairly entertaining song about the MVC pattern from here!

您在 Swing 编程中可能面临的一个问题是将 SwingWorker 和 EventDispatch 线程与 MVC 模式合并.根据您的程序,您的视图或控制器可能必须扩展 SwingWorker 并覆盖放置资源密集型逻辑的 doInBackground() 方法.这可以很容易地与典型的 MVC 模式融合,并且是 Swing 应用程序的典型特征.

One issue you may face with Swing programming involves amalgamating the SwingWorker and EventDispatch thread with the MVC pattern. Depending on your program, your view or controller might have to extend the SwingWorker and override the doInBackground() method where resource intensive logic is placed. This can be easily fused with the typical MVC pattern, and is typical of Swing applications.

编辑 #1:

此外,将 MVC 视为各种模式的一种组合很重要.例如,您的模型可以使用观察者模式实现(要求将视图注册为模型的观察者),而您的控制器可能使用策略模式.

Additionally, it is important to consider MVC as a sort of composite of various patterns. For example, your model could be implemented using the Observer pattern (requiring the View to be registered as an observer to the model) while your controller might use the Strategy pattern.

编辑#2:

我还想具体回答您的问题.您应该在视图中显示您的表格按钮等,这显然会实现一个 ActionListener.在您的 actionPerformed() 方法中,您检测事件并将其发送到控制器中的相关方法(请记住 - 视图持有对控制器的引用).所以当一个按钮被点击时,事件被视图检测到,发送到控制器的方法,控制器可能会直接要求视图禁用按钮或其他东西.接下来,控制器将与模型交互并修改模型(其中主要有 getter 和 setter 方法,以及其他一些用于注册和通知观察者等的方法).一旦模型被修改,它就会对注册的观察者调用更新(这将是您的情况下的视图).因此,视图现在将自行更新.

I would additionally like to answer specifically your question. You should display your table buttons, etc in the View, which would obviously implement an ActionListener. In your actionPerformed() method, you detect the event and send it to a related method in the controller (remember- the view holds a reference to the controller). So when a button is clicked, the event is detected by the view, sent to the controller's method, the controller might directly ask the view to disable the button or something. Next, the controller will interact with and modify the model (which will mostly have getter and setter methods, and some other ones to register and notify observers and so on). As soon as the model is modified, it will call an update on registered observers (this will be the view in your case). Hence, the view will now update itself.

这篇关于MVC 模式和 Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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