Many times there is the need to load external resources into the program, may they be graphics, audio samples or text strings.
Is there a patten for handling the loading and the handling of such resources?
For example: should I have a class that loads all the data and then call it everytime I need the data? As in:
GraphicsHandler.instance().loadAllData()
...//and then later:
draw(x,y, GraphicsHandler.instance().getData(WATER_IMAGE))
//or maybe
draw(x,y, GraphicsHandler.instance().WATER_IMAGE)
Or should I assign each resource to the class where it belongs?
As in (for example, in a game):
Graphics g = GraphicsLoader.load(CHAR01);
Character c = new Character(..., g);
...
c.draw();
Generally speaking which of these two is the more robust solution?
GraphicsHandler.instance().getData(WATER_IMAGE)
//or
GraphicsHandler.instance().WATER_IMAGE //a constant reference
I don’t know that either option is going to be particularly robust. In both cases, you’re relying on some kind of singleton or static-state-heavy implementation to manage what amount to global variables. How exactly you access the global variables (whether directly or hidden behind some kind of weakly typed “getData()” call) isn’t going to matter a lot to maintainability in the face of a global variable scheme in general. Your application is going to be highly cross-coupled and interdependent either way.
I would say a decent option with an eye on future flexibility would be to take your getData() concept and put it in an interface. Then inject an instance of that interface into classes that need access to the data (and try to limit that number as much as possible). This way you can mock out or swap the thing that provides that data. The way you have it now, this is impossible — every client of that singleton depends on the entire application state being spun up in order for it to be used, making testing and decoupling difficult, if not impossible. And that state of affairs is the opposite of robust.
It depends on the purpose of your application.
In some games, there is a need for a highly responsive user interface and loading resources while playing could have a negative impact. Secondly, many games run with 100% CPU usage, esp. 3D shooters, so that any data which can be preloaded also becomes preloaded. This lowers the overall system requirements. As can be seen, there are performance reasons for preloading of resources. Finally, resources are often compressed inside one large archive to save disk space. It takes some time to decompress all files for the current level. That is the last reason for preloading.
Is the program a game? Does it need some of these performance-optimization techniques? Otherwise, the closest design pattern for this would be Facade:
http://en.wikipedia.org/wiki/Facade_pattern
Gfx ghost = (Gfx)ResourceLoader.load("/gfx/sprites/ghost");
Sound taunt = (Sound)ResourceLoader.load("/audio/boost/you_play_like_a_girl.voc");
Music levelMusic = (Music)ResourceLoader.load("/audio/music/level1.mp3");
With the solution above, there can be a resource cache and you want to log most frequently loaded resources. After that, you can preload those resources implicitly after your program starts because you know they will be needed. You should not however preload everything. For every level, different resources are needed.
1