is it a singleton ?

Started by
1 comment, last by CTar 16 years, 9 months ago
I've been reading about them and I happen to have a class that has no reason to be instanciated more than once (it just creates the windows and renders stuff), does that fact alone make it a "singleton" even without any kind of restricting code ?
Advertisement
A singleton is not whether you create something only once or not, it's a design pattern. This means the restricting code/design is basically the singleton. See Singleton and Design Pattern.
The singleton anti-pattern isn't just about having a single instance. It consists of two parts,
1) ensuring that only one instance can be created
2) ensuring global access
If you have a single global variable, and mentions in the documentation that only a single instance should ever be created then you have a singleton. Your method of ensuring one instance is just questionable. If however you make no attempt to ensure that single instance then you won't have a singleton, just a variable.

That doesn't mean you should try to make any kind of restricting code or make your variable global. Singletons are usually a sign a of bad design and creating a single instance of a class is usually a much better solution.

This topic is closed to new replies.

Advertisement