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 difference being that with a static class you don't / can't instance it, you just use it such as Math.pi()
. With a singleton class, you would still need to do something like
singleton firstSingleton = new singleton();
firstSingleton.set_name("foo");
singleton secondSingleton = new singleton();
Correct me if i am wrong, but firstSingleton == secondSingleton
right now, yes?
secondSingleston.set_name("bar");
firstSingleton.report_name(); // will output "bar" won't it?
Please note, I am asking this language independently, more about the concept. So I am not worried about actually how to code such a class, but more why you would wan't to and what thing you would need to consider.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…