Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
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? –  Mike Partridge Jan 17 '13 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 Jan 18 '13 at 6:57
add comment

1 Answer

up vote 1 down vote accepted

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);
    }
}
share|improve this answer
    
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 Mar 1 '13 at 6:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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