I am confused when I read this (regarding singleton design pattern):

How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects.

So what is the use of singleton pattern if we can create multiple instances?


Design Patterns - Elements Of Reusable Object Oriented Software (1995) - Gamma, Helm, Johnson, Vl

  • 6
    I am sure there is something after that saying "That's why the constructor is private".
    – Rig
    Commented Apr 11, 2013 at 14:54
  • The "create no more than one instance" is usually a bogus requirement. You create only one instance if you need only one. But you won't die if more than one instance is created. Otherwise, you should rethink the design of the class.
    – donquixote
    Commented Jul 9, 2014 at 16:01

4 Answers 4


Without the full text this is not sure, but my (somehow educated) guess:

They only warn that a global variable is not the right way to ensure that you have a singleton. The following text should then show how to do this inside the class that should be a singleton.

  • Yeah..thanks..so silly of me misunderstood that, exactly after this it says something else..Was so excited to know that's why din't read anything further :)
    – joey rohan
    Commented Apr 11, 2013 at 14:55

In the quote they don't talk about how to do it, but how not to do it.

Some approach for getting a singleton is to make the constructor private and write a own static method, that creates a new element on the first call, saves it in a static variable and always returns this object when called again.


There are very few languages that prevent you from shooting your foot if you really want to do it. The singleton pattern makes maintaining exactly one instance of the class quite trivial and creating multiple instances of it quite difficult; creating multiple instances unintentionally - impossible.

Most design patterns are to make programmer's life easier and remove their headaches. They can be circumvented and exceptions to them can be made. The singleton is to remove the headache of watching when and where you instantiate a class there should be only one instance of.

It's the kind of thinking "I'll catch cold on purpose, to spite my parents" if you try to create more than one instance of a class designated as singleton. Adult people don't do this.


In case of a singelton class. I can create multipe object by using Malloc. For example:-

Singeltone* ptr = (Singeltone*) malloc(sizeof(Singeltone));

  • 2
    Welcome to programmers.SE! I believe the OP had trouble understanding the concept of the singleton pattern, and not an issue with finding a way to create multiple instances. Try answering what the user specifically asked. Commented May 29, 2013 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.