Purpose of singletons in programming

Posted by thecoshman on Stack Overflow See other posts from Stack Overflow or by thecoshman
Published on 2010-03-31T07:23:59Z Indexed on 2010/03/31 7:33 UTC
Read the original article Hit count: 188

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about singleton

Related posts about concepts