Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a commonly used OO Pattern for holding "constant variables"?
    primarykey
    data
    text
    <p>I am working on a little <a href="http://mirror.mit-ocw.sbu.ac.ir/OcwWeb/Electrical-Engineering-and-Computer-Science/6-170Fall-2005/Projects/index.htm" rel="nofollow noreferrer">pinball-game project</a> for a hobby and am looking for a pattern to encapsulate constant variables.</p> <p>I have a model, within which there are values which will be constant over the life of that model e.g. maximum speed/maximum gravity etc. Throughout the GUI and other areas these values are required in order to correctly validate input. Currently they are included either as references to a <code>public static final</code>, or just plain hard-coded. I'd like to encapsulate these "constant variables" in an object which can be injected into the model, and retrieved by the view/controller.</p> <p>To clarify, the value of the "constant variables" may not necessarily be defined at compile-time, they could come from reading in a file; user input etc. What is known at compile time is <em>which</em> ones are needed. A way which may be easier to explain it is that whatever this encapsulation is, the values it provides are <strong>immutable</strong>.</p> <p>I'm looking for a way to achieve this which: </p> <ul> <li>has compile time type-safety (i.e. not mapping a string to variable at runtime)</li> <li>avoids anything static (including enums, which can't be extended)</li> </ul> <p>I know I could define an interface which has the methods such as: </p> <pre><code> public int getMaximumSpeed(); public int getMaximumGravity(); </code></pre> <p>... and inject an instance of that into the model, and make it accessible in some way. However, this results in a lot of boilerplate code, which is pretty tedious to write/test etc (I am doing this for funsies :-)).</p> <p>I am looking for a better way to do this, preferably something which has the benefits of being part of a shared vocabulary, as with design patterns. </p> <p>Is there a better way to do this?</p> <p>P.S. I've thought some more about this, and the best trade-off I could find would be to have something like: </p> <pre><code>public class Variables { enum Variable { MaxSpeed(100), MaxGravity(10) Variable(Object variableValue) { // assign value to field, provide getter etc. } } public Object getVariable(Variable v) { // look up enum and get member } } // end of MyVariables </code></pre> <p>I could then do something like: </p> <pre><code>Model m = new Model(new Variables()); </code></pre> <p><em>Advantages</em>: the lookup of a variable is protected by having to be a member of the enum in order to compile, variables can be added with little extra code<br> <em>Disadvantages</em>: enums cannot be extended, brittleness (a recompile is needed to add a variable), variable values would have to be cast from Object (to Integer in this example), which again isn't type safe, though generics may be an option for that... somehow</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload