1

I have an initialization class that preloads content into a variable (probably a list or array). There will only be one instance of this initialization class but there will be many classes that need to access the preloaded content.

The problem is not many of them are related and none of them extend my initialization class. I thought about this for a bit and decided on using a static method and variable for this use. So something like this...

public class InitClass
{
     static List PreloadedContent;

     static ModelData GetContent(String ContentName)
     {
          //return the preloaded content that matches given name
     }
}

The preloaded content may at some time decrease or increase in size depending on what the situation may call for. I've run into situations where something like this has been the only decent looking solution however; I think its an ugly solution.

Note: I can't load the data onto a class that needs it when it is created due to a variety of reasons - most of which are reasons I don't know about yet but will most likely come up. Certain classes will be loaded/unloaded depending on the rendering of the scene and my InitClass won't handle the creation of these objects most of the time.

Can anyone give me a better solution?

2 Answers2

1

what you are doing is known as singleton. here are some previous discussions on this:

Community
  • 1
  • 1
Owen
  • 82,995
  • 21
  • 120
  • 115
0

To avoid static/global scope you could use some kind of Registry class. This means you have one class which you initialize at program startup. This class holds references to all other classes that need to be accessed globally. Now you pass the initialized instance of your registry class to all instances in your application. It isn't a very pretty soluation, but for me it is the best. With Static and global variables I always ended up in having some problems when testing or debugging code.

Another aproach would be to use a Singleton. Since they also just hold a static instance I would not prefer them.

okoman
  • 5,529
  • 11
  • 41
  • 45