I have a Compressor component in my model:
public class Compressor extends MComponent {
public static final double THRESHOLD_MAX = 1;
public static final double THRESHOLD_MIN = 0;
public static final double THRESHOLD_DEFAULT = 1;
private double threshold;
/* <snip> - many other parameters*/
public void setThreshold(double v) { this.threshold = v; }
}
And for this particular application, I have a GUI I’m constructing with Java Swing, which adds sliders, buttons etc to display and control the Compressors parameters.
Now some of these sliders should operate on a linear scale, while others should operate on a logarithmic/expontial scale (where a move of the slider will make a small change of value in the lower end, and a large change of value at the upper end).
I have a GuiConstructor class that handily creates sliders and and their move events, so that they’ll act appropriately given a Min value, max value, Exponential/Linear type, and a q value (for exponential types).
gc.addSliderPanel(
panel,
"Threshold",
Compressor.THRESHOLD_MIN,
Compressor.THRESHOLD_MAX,
c.getThreshold(),
GuiConstructor.Scale.LINEAR, /*<-- this line*/
0,
new SetThreshold(c),
DEFAULT_SLIDER_GRAINS,
GuiConstructor.SliderValueType.FLOAT);
The question is – should I be declaring the display type in the view or would be ok to specify the display type in the model?
eg.
public class Compressor extends MComponent {
public static final double THRESHOLD_MAX = 1;
public static final double THRESHOLD_MIN = 0;
public static final double THRESHOLD_DEFAULT = 1;
public static final GuiConstructor.Scale THRESHOLD_SCALETYPE = GuiConstructor.Scale.LINEAR;
private double threshold;
I like the idea of specifying it in the model, as I only need to think about the display parameters when I create each component, rather than when I’m putting it together.
A model can most certainly contain attributes that describe how the model works: that it is a linear vs. exponential compressor. However, it should never have a direct reference to a specific view object such as you propose. Instead, make the attribute descriptive in nature, and have the “GuiConstructor” select the right view based on the attribute’s value.
For instance:
The overriding principle, is that the Model should have no knowledge of the view. The view object should be changeable without the model needing to change. Or to put it another way, assume that an entirely different set of UI classes are built. The model class should not require a coding change.
Here are a couple of friendly observations on your code:
Hope this helps,
john…