1

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, ImageWidgetand ButtonWidget.

Since nesting is possible, a ButtonWidgetis 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
  • 3
    Could you have your widgets ask their parent for the AssetLoader if they don't have one themselves, then just set it on the top-level widget? Commented Jan 17, 2013 at 16:59
  • Hm, that sounds like an idea. I don't pass a parent to the widgets yet, but I could certainly do so. That's similar to Android in fact, I could have some context interface that is implemented by Widget, which returns the associated AssetLoader.
    – futlib
    Commented Jan 18, 2013 at 6:57

1 Answer 1

1

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
  • Thought about this quite a while, but yes, this really seems like the best solution, as I have to pass the parent into every widget for other reasons now anyway. I'm a bit stumped that there's no ready-to-use design pattern though.
    – futlib
    Commented Mar 1, 2013 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.