问题
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