Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 118513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:30:23+00:00 2026-05-11T03:30:23+00:00

I am working on a little pinball-game project for a hobby and am looking

  • 0

I am working on a little pinball-game project for a hobby and am looking for a pattern to encapsulate constant variables.

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 public static final, 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.

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 which ones are needed. A way which may be easier to explain it is that whatever this encapsulation is, the values it provides are immutable.

I’m looking for a way to achieve this which:

  • has compile time type-safety (i.e. not mapping a string to variable at runtime)
  • avoids anything static (including enums, which can’t be extended)

I know I could define an interface which has the methods such as:

 public int getMaximumSpeed();  public int getMaximumGravity(); 

… 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 :-)).

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.

Is there a better way to do this?

P.S. I’ve thought some more about this, and the best trade-off I could find would be to have something like:

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 

I could then do something like:

Model m = new Model(new Variables()); 

Advantages: 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
Disadvantages: 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:30:24+00:00Added an answer on May 11, 2026 at 3:30 am

    Are you looking for the Singleton or, a variant, the Monostate? If not, how does that pattern fail your needs?

    Of course, here’s the mandatory disclaimer that Anything Global Is Evil.

    UPDATE: I did some looking, because I’ve been having similar debates/issues. I stumbled across a list of ‘alternatives’ to classic global/scope solutions. Thought I’d share.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 74k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I'm not sure if there are any plugins out there… May 11, 2026 at 2:13 pm
  • added an answer Yes, it is worth worrying about in practice. MD5 is… May 11, 2026 at 2:13 pm
  • added an answer is user-row-1 the ID of the table row? If yes,… May 11, 2026 at 2:13 pm

Related Questions

I am working on a little Perl module and for some reason I had
I am working on a web application using Python (Django) and would like to
I am working on a Office Word add-in for Word 2003. When I reopen
I am working on a program that needs to create a multiple temporary folders
I am working on a small AIR desktop application and I have some configuration
I am working on a collection of classes used for video playback and recording.
I am working on a very large scale computing library that is using STL

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.