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

查看:100
本文介绍了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中。但是,是不是混合了模式的Controller和View?

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?

据我所知,我设法在查看时正确实现了MVC模式在JTable(和模型),但当我整个看整个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)?

推荐答案

我强烈推荐给你的摇摆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.



< a href =https://rads.stackoverflow.com/amzn/click/0596007124\"rel =noreferrer>来源(如果你想知道什么是奶油控制器,想想奥利奥饼干,控制器是奶油中心,视图是顶部饼干,模型是底部饼干。)

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.)

嗯,如果你有兴趣,你可以从这里下载一首关于MVC模式的相当有趣的歌曲!

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

Swing编程可能遇到的一个问题涉及使用MVC模式合并SwingWorker和EventDispatch线程。根据您的程序,您的视图或控制器可能必须扩展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视为各种模式的复合是很重要的。例如,您的模型可以使用Observer模式实现(要求View注册为模型的观察者),而您的控制器可能使用策略模式。

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

我还想特别回答你的问题。您应该在View中显示表按钮等,这显然会实现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天全站免登陆