如何在Java Swing中正确实现MVC?

15
如果您需要更多细节,请告诉我,或者参考这个问题的最后几行。我已经阅读了很多资料,但感觉把简单的东西变得复杂了,仍然有些地方卡住了,也许您可以在那些非常具体的点上帮助我。
我正在使用Netbeans IDE 7和JDK 7,没有框架。第一个窗口是JFrame,所有其他窗口都是JDialog,modal=true。
问题:
  1. How do I correctly implement the MVC pattern with swing? From the ideas bellow Which one is better: (A) or (B)? Or maybe another one... Why is it better?

    (A) Main:

    MyModel model
    MyView view(model)
    

    MyView:

    MyController(this, model)
    

    (B)
    Main:

    MyModel model
    MyView View
    MyController controller(view, model)
    
  2. when I click jbutton1 in MainFrame I need it to open the SettingsFrame for editing settings. where should I instantiate the View, the Model and the Controller of the SettingsFrame? In MainFrame Controller?

  3. In terms of MVC organization and implementation, how should I handle more specific features that (apparently) lacks one or two of the MVC "legs" (either Model or View or Controller)? Should I create empty classes for them?

    a. The implementation of a TrayIcon
    b. A URL connection class (an HttpsUrlConnection which will update data in the main jframe and also upload/download files)
    c. A Directory Monitor (which will update data in the main jframe and also use the urlconnection to download a file)
    d. My own implementation of TableModel
    e. json
    
  4. How to correctly keep and use an object with settings through the whole application? I will need it's information in different places (Views, Models, Controllers) but it might be altered by user during the runtime). Is it a good idea to make this model a singleton?

  5. What should I do when:

    a. View needs some data from the Model? 
    What I'm doing: using the reference of Model which I keep in the View
    b. View needs some data from the Controller?
    What I'm doing: using the reference of Controller which I keep in the View
    c. Model needs some data from the Controller?
    Still didn't happen but I have no idea how to do correctly
    d. Model needs some data from the View?
    What I'm doing: pulling all my hair from my head...
    e. Controller needs some data from the View?
    What I'm doing: using the reference of the View which I keep in the Controller
    f. Controller needs some data from the Model?
    What I'm doing: using the reference of the Model which I keep in the Controller
    g. One of FooModel, FooView or FooController needs data from one of BarModel, BarView or BarController?
    What I'm doing: thinking of jumping from the highest building...
    
  6. Any hints on how to know if I implemented MVC correctly? Should I process massive data in Model or Controller?

  7. I'm also using a DAO, what I'm doing is: my model has a

    ArrayList MyModel load()

    method which creates an instance of the DAO and returns the ArrayList of Models returned by the DAO, and then sometimes I process this ArrayList of Models in the Model and sometimes I allow the Controller to process it. Is this a good practice or is there a better way? By Process I mean: iterate through the ArrayList and get the data from the models.

  8. I Have a PasswordCheck jDialog to restrict access to some Views. How can I reuse it in terms of MVC, so that I can use the same PasswordCheck dialog for allowing/restricting access to different Views without doing a mess in the code?

  9. Any other tips, hints, ideas, suggestions?

背景: 我需要在短时间内开发一个Java Swing MVC软件,但默认情况下我不是Java开发人员,也不太习惯实现MVC模式,尤其是在Java中(我了解这个概念,但有时缺乏实现类之间关系的知识)。 该应用程序基本上是一个监视本地/在线文件的监视器,在主窗口中使用JTable来显示此数据。我正在使用新的WatchService API来跟踪本地文件,并使用DAO将它们的信息保存在h2数据库中,然后重新加载此数据到主框架jtable中。我还必须通知用户有关新文件的信息(为此我使用TrayIcon)。对于在线文件的监视/上传/下载,我使用HttpsUrlConnection和json。它还可以允许设置自定义化。

非常感谢您的时间和帮助。

1个回答

7
请看Sun(Oracle)的建议
为简化起见,您可以使每个组件(模型、视图、控制器)向顶层应用程序组件注册,提供单一参考点,而不是在每个组件之间进行单独引用(您的A或B)。我引用的文章提供了推拉设计的想法;我建议使用推作为更流行的现代方法。披露:我有Java和MVC的经验,但不是Swing中的MVC。
“SettingsFrame”的View、Model和Controller应该在顶层应用程序组件中实例化。
我会将仅GUI部分实现为自己的GUI库。纯算法/服务部分则实现为服务库。
数据处理算法很适合放在控制器甚至服务库中;您的模型不应做太多处理,只能进行数据类型转换或验证。
如何正确地保持和使用具有设置的对象?请参阅我的注册说明;单例可能是适当的。

+1,我很喜欢所提出的想法,就像我很喜欢这个链接一样,这也将有助于我的努力 :-) 对这个问题的赞同也是 +1,讲解得非常好,我是第一个点赞的人 :-) - nIcE cOw
4
+1 请参考这个示例,它遵循了与引用的图表类似的大纲 - trashgod
1
@dcr,关于我的注册笔记,我只是想说您可能希望将“每个组件(模型、视图、控制器)”都注册到一个顶级应用程序组件中,以提供单一的参考点。因此,只有一个顶级应用程序对象是共享的;设置或其他公共数据可以通过应用程序访问。优点包括减少复制引用和在运行时更轻松地替换实现;甚至可以使用Spring或其他IOC机制。 - Will
@trashgod 非常感谢提供示例。在寻找MVC时,我没有找到这个样本。我会尽快仔细阅读。+1 - dcr
@Will 哦,好的,一开始我以为你是在提到某种外部笔记,但现在我明白了。所以从阅读中,关于我的第5个问题,我理解我可以将控制器注册为模型观察者,以便在模型更改时更新视图,或者我可以将视图注册为模型观察者并直接更新它,对吗?(我还在阅读,但我正在努力理解)。 - dcr
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接