-1

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?

SOURCE:

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

2
  • 6
    I am sure there is something after that saying "That's why the constructor is private".
    – Rig
    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
    Jul 9, 2014 at 16:01

4 Answers 4

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.

1
  • 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
    Apr 11, 2013 at 14:55
2

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.

0
2

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.

-2

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

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

1
  • 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. May 29, 2013 at 7:07

Your Answer

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

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