I’m making a game in java, and I’m stuck with a design problem.
My resources (images, animations, sounds) are stored in a few HashMaps, one for each type of resource. These (static) hashmaps are located in a static class called “Res”. When an entity needs a resource, it accesses one of the hashmaps of the global class, and if the resource doesn’t exist, it is automatically loaded.
static Map<String, Sprite> sprites = new HashMap<>();
static Map<String, BufferedImage> images = new HashMap<>();
static Map<String, Clip> sounds = new HashMap<>();
static Map<String, Font> fonts = new HashMap<>();
My question is: Is this design good enough? I’ve read that static functions are bad practice, but do I have to pass an instance of the class “Res” everytime then? Or are there other alternatives? And also, is this resource management system good practice?
Thanks in advance!
Use Singleton to maintain all of the resources instead of these static functions.
}
To use the resource you can just call