How to properly design an MV application in Java Swing?

十年热恋 提交于 2019-12-11 13:18:41

问题


I'm trying to design an MV (Model-ViewController) application in Java swing. I'm having trouble with assigning the correct model to each ViewController.

Here's my current design (not the actual names) :

The idea is that I have two views : the Gui and a Midi Device.

Each view has its corresponding model. However, a part of the Gui (ButtonBar) needs to access the midi device model.

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.

What better way do you suggest ?


回答1:


One approach is to let the device view and/or model export the Action instances required by the ButtonBar. 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; listening views update themselves in response. This example creates a JToolBar using such actions. Finally, JHotDraw, cited here, generates actions dynamically for use in a tool bar, as discussed here.



来源:https://stackoverflow.com/questions/37884313/how-to-properly-design-an-mv-application-in-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!