Question about singleton classes and when to use them.

Hi, I'm in the first year of a programming degree and although we are due to cover this area after the Easter break I am interested in perhaps implementing a singleton class into one of the projects we have been set.

The project is to make a snake game in c++. I have a level class that is loaded when the player chooses the level they wish to play. In reality there is always a level loaded. The menu is another type of level that has additional properties such as controls(buttons etc) and a background demo level playing with AI snakes.

What I am wandering is should the level class be implemented as a singleton class? There are many times in the code where an object in the level needs to know something about the level it's a part of (such as grid dimensions/cell size) or to get a reference to another object in the level. Since there is only ever meant to be one instance of a level at a time would it make more sense for it to be a singleton with a method to get its instance rather than saving a pointer to the level in the classes of objects that need to know about it?

[Edit] Another thing does a singleton class have to be loaded throughout the entire life of the application? Can you have a singleton class with different variables - this is the reason I am thinking it might not be a suitable idea. Maybe a better idea would be a LevelManager class that is singleton and as a reference to the current loaded level as well as other generic functionality or maybe its not suitable at all.

[Edit2]After reading this: http://stackoverflow.com/questions/86582/singleton-how-should-it-be-used

I realised my idea of singletons is miss founded. I'll wait until we cover them I think.

Thanks.
Last edited on
Topic archived. No new replies allowed.