Purpose of singletons in programming
- by thecoshman
This is admittedly a rather loose question. My current understanding of singletons is that they are a class that you set up in such a way that only one instance is ever created.
This sounds a lot like a static class to me. The main differnce being that with a static class you don't / can't instance it, you just use it such as Math.pi(). With a singletong class, you would still need to do something like
singleton mySingleton = new singleton();
mysingleton.set_name("foo");
singleton otherSingleton = new singleton();
// correct me if i am wrong, but mysingleton == othersingleton right now, yes?
// this the following should happen?
otherSingleston.set_name("bar");
mysingleton.report_name(); // will output "bar" won't it?
Please note, I am asking this language independently, more about the concept. So I am not so worried about actually how to coed such a class, but more why you would wan't to and what thing you would need to consider.