From a beginning C++ user’s point of view the only reason I would see a singleton class useful is similar to when I want to use a static variable. Do you guys know when its most appropriate to use a singleton class?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only time you should use a singleton is in the rare case where it is VITAL that there only ever be one of these objects. This simply doesn’t come up nearly as often as you might think.
Quite often it’s more than sufficient to just make a single instance of the class in question, but not put in all the machinery required to enforce the uniqueness of the object.
Every time I’ve used a singleton (or found one in the codebase I work with) I’ve ended up removing it because I needed to change the behavior, and the singleton machinery got in the way. I’ve yet to see a case in our code (payment processing) where a singleton actually made sense. I’ve got more than a few cases where I’ve only got one instance of a particular class, but none where enforcing that through the singleton pattern was necessary or helpful.