DI for assetManager

In my view class, I wish to use the Application's assetManager in order to load materials, etc.



What's the idiomatic way to inject the assetManager to all the classes which needs it? I'll hate adding it as a constructor parameter for each and every class that uses assets.

elazarl said:

In my view class, I wish to use the Application's assetManager in order to load materials, etc.

What's the idiomatic way to inject the assetManager to all the classes which needs it? I'll hate adding it as a constructor parameter for each and every class that uses assets.

A view class should normally not perform operations, look into MVC style programming to separate view and model with a controller. So each view class gets a controller that has a link to the assetmanager. If you want just a simple solution to access *one* global assetmanager, what about some static method to get it?

In web applications, the view defines how the web page looks.



In my design, the view class defines how the spaceship looks (it might have little ships circling it, so it might not be as simple as loading a model). In order to display the ship with its little ships for decoration, I need to load textures. In order to load textures I need assetManager.



Am I wrong about it?



I can have an assetManager object in each View class, and send it through the constructor.



But since the assetManager is relevant to all view classes, it seems kind of redundant.

You can always expose the AssetManager though some static method. The reason it is not a utility (e.g static) class is because you may want to have several AssetManagers at the same time.