I've read the questions on S.O. regarding Singleton and just watched an hour long google tech talk. As far as I can tell, the consensus in the OO world seems to be that singletons are more of an anti-pattern rather than a useful design pattern.
That said, I am interviewing these days and the question comes up a lot--what is a singleton, and how would you use it?
What is the best way to answer this question? Should I simply describe the design pattern and then say the only acceptable use I've heard of is for logging, and that it is often mis-used for global state?
Tricky. There are plenty of people who feel that singletons are essentially an anti-pattern like you said (me included), but there are also a lot who feel that it is simply an OOP-acceptable way to do globals.
If the interviewer is in that camp then yes, I think he's wrong, but that might not be the best thing to say during the interview. ;)
So I'd probably try to be neutral and stick to the facts. You don't know which camp your interviewer falls in, so stick to the indisputable facts. What does a singleton do? And to demonstrate usage, stick to the few cases where most people can agree that a singleton is an acceptable answer. Or explain your experience (since people can't disagree with that either).
But whether you're "for" or "against" singletons, an interview is probably not the right time to crusade for that cause. ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With