I have been reading recently that Singletons are often a design pattern that are abused. I understand that globals can be bad and that singletons are not extendable (thus not good OO design), but I was wondering if in this instance I am using it 'correctly'.
Essentially, I have some software running on a - lets say - vending machine. It has an administrator console where you can login and configure some stuff. For example, today I want a coke bottle to cost $3 and a sprite to cost $2, so I can just log in to this admin console and edit these values. Then, in my code, I can read these configuration parameters from the console using some java code:
AdminConsole adminConsole = new AdminConsole();
Map<String, Int> drinkPriceMap = adminConsole.getSettings();
This loads a map of key value pairs of things like Coke:3, Sprite:2, Water:2 and will happen when the 'main' class is first instantiated on machine-bootup.
The prices of the drinks should only be updated per hardware reset.
Now, because the hardware of this vending machine is very limited, and pulling the settings from the console is quite memory-heavy for it, I want to do things with this in mind.
When the AdminConsole is instantiated it pulls these values from the admin console and generates the map. The act of pulling these values is a memory expensive operation, whereas the map is not such a big deal. What I want to do in the code is therefore make the AdminConsole a singleton so it can only be instantiated once. This is so that when a new developer joins the team and is unaware that it is a memory-expensive operation on a memory-limited hardware, he will not accidentally instantiate it multiple times.
Instead, it would look something like this:
AdminConsole adminConsole = AdminConsole.getInstance();
Map<String, Int> drinkPriceMap = adminConsole.getSettings();
So, do you think think I am using the singleton in a good manner here, or do you think there is a better way I could go about doing this?