I’m working on a simple UI system for a game. The building blocks are Widget
objects, which can contain each other. There are several sub classes of Widget
, e.g. LabelWidget
, ImageWidget
and ButtonWidget
.
Since nesting is possible, a ButtonWidget
is really nothing but a container for a LabelWidget
and an ImageWidget
. I’m pretty happy with that.
The problem is: I now need to use an AssetLoader
to load images, I cannot get them directly from the disk. There are multiple AssetLoader
implementations, one for each platform the game runs on, and there is only one instance which is created early on. And it has state.
So now I need to pass an AssetLoader
object into ImageWidget
. And into ButtonWidget
, since it contains an ImageWidget
. And so on. I’ve updated the code and now I need to pass an asset loader into almost every widget, and I really don’t like what I see.
I’ve investigated some code bases of UI frameworks I like, and noticed that Android has something similar: There is a Context
object which is passed into every view, which can be used to load resources. But since views there are usually instantiated magically from XML, it’s not as annoying to use as what I have.
I can think of only one way out: Make a singleton that does nothing but offer a reference to the asset loader, e.g. AssetLoaderHolder
. But I’m not convinced that this isn’t actually worse than what I have.
Can you think of any remotely elegant solution for this? Note that using a dependency injection framework is not an option for me.
2
If all widgets have a reference to their container widget, implement a method to ask the container for the AssetLoader
or Context
if they don’t have their own; then you can just set it on the top-level container widget and it’ll be available to the whole hierarchy.
There are variations on how it could be implemented, but here’s a simple version in Java:
public abstract class Widget {
private Widget container;
private AssetLoader assetLoader;
protected AssetLoader getAssetLoader() {
if (assetLoader == null) {
return container.getAssetLoader();
}
return assetLoader;
}
}
public class LabelWidget extends Widget {
private String labelKey;
public String getLabel() {
getAssetLoader().getLabel(labelKey);
}
}
1