I am writing a program that allows a user to interact with MIDI-connected devices. Upon connecting a MIDI device, interactions with this device will update a GUI. Updates to the GUI include what keys are being pressed, if the volume is turned up/down, etc. In addition, the user can "request" the status of MIDI components through the GUI (how many MIDI devices are available, opening/closing MIDI devices).
All of this is set up using the Mediator pattern, and I would like to ask you all the best way of sending "requests" to each of these classes; MidiController
(controls MIDI-related tasks) and GUI
(which shows MIDI-related events).
A button exists on the GUI to search for MIDI-connected devices, upon clicking it, the MidiController
will get some message (from the Mediator pattern) to find MIDI-connected devices.
My code currently looks like this for this function:
@Override
public void receive(String message) {
System.out.println("Message from GUI: " + message);
switch(message){
case "FIND_ALL_MIDI_DEVICES":
devices = MidiSystem.getMidiDeviceInfo();
break;
default:
break;
}
}
I've opted to use a switch
statement, but I feel there is a better option. I am expecting this class, MidiController
to respond to many different messages. Is there a clean way I might communicate sending data / requests / updates between these two classes?
-
\$\begingroup\$ Try Abstract Factory pattern tutorialspoint.com/design_pattern/abstract_factory_pattern.htm \$\endgroup\$Eugene Ustimenko– Eugene Ustimenko2015年12月28日 13:10:55 +00:00Commented Dec 28, 2015 at 13:10
1 Answer 1
Since you only support a limited subset of messages you might want to consider using an enum
to represent the different kinds of messages.
I think you're building yourself a god-class though. Instead of having everything running through the MidiController
(which makes the Mediator Pattern basically useless) you could just as well couple GUI and Controller directly.
Interestingly that's the more often used approach, to have the Controller take the role described as Mediator in the link you provided. This is especially interesting because it is not guaranteed that you have similar "suppliers of information" for your GUI.
I think the Mediator Pattern overall is the wrong pattern for your situation :/
-
\$\begingroup\$ Can you elaborate on the "suppliers of information" you make reference to? \$\endgroup\$Zac– Zac2015年12月30日 03:49:18 +00:00Commented Dec 30, 2015 at 3:49
-
\$\begingroup\$ suppliers of information as in "source". You need to get the data you display in your UI from somewhere, right? Why do you assume all these sources (or suppliers) of information follow the same structure? \$\endgroup\$Vogel612– Vogel6122015年12月30日 20:28:01 +00:00Commented Dec 30, 2015 at 20:28