简体   繁体   English

如何在Java Swing中正确设计MV应用程序?

[英]How to properly design an MV application in Java Swing?

I'm trying to design an MV (Model-ViewController) application in Java swing. 我正在尝试使用Java swing设计MV(模型-ViewController)应用程序。 I'm having trouble with assigning the correct model to each ViewController. 我在为每个ViewController分配正确的模型时遇到麻烦。

Here's my current design (not the actual names) : 这是我目前的设计(不是实际名称): 类图

The idea is that I have two views : the Gui and a Midi Device. 我的想法是,我有两种看法:Gui和Midi Device。

Each view has its corresponding model. 每个视图都有其对应的模型。 However, a part of the Gui (ButtonBar) needs to access the midi device model. 但是,Gui(ButtonBar)的一部分需要访问Midi设备模型。

For now, my solution is to pass the MidiDevice model to the MainWindow's constructor, then let it pass it down to the MyPanel constructor, then to the ButtonBar... To me, that smells bad design and future complications. 现在,我的解决方案是将MidiDevice模型传递给MainWindow的构造函数,然后再将其传递给MyPanel构造函数,然后传递给ButtonBar ...对我来说,这感觉到糟糕的设计和未来的复杂性。

What better way do you suggest ? 您建议什么更好的方法?

One approach is to let the device view and/or model export the Action instances required by the ButtonBar . 一种方法是让设备查看和/或模型导出ButtonBar所需的Action实例。 In this simple example , the view exports actions used to control the display itself. 在这个简单的示例中 ,视图导出用于控制显示本身的动作。 In the more complex examples cited here , actions provided by EditorKit subclasses operate on the Document model used by text components; 这里引用的更复杂的示例中, EditorKit子类提供的操作在文本组件使用的Document模型上运行; listening views update themselves in response. 听众的意见会根据自己的情况进行更新。 This example creates a JToolBar using such actions. 示例使用此类操作创建一个JToolBar Finally, JHotDraw , cited here , generates actions dynamically for use in a tool bar, as discussed here . 最后, JHotDraw ,引在这里 ,生成动态的行动在工具栏的使用,为讨论在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM