Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
6  
I am sure there is something after that saying "That's why the constructor is private". –  Rig Apr 11 '13 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 at 16:01

4 Answers 4

up vote 4 down vote accepted

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.

share|improve this answer
    
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 '13 at 14:55

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

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

share|improve this answer
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. –  Rafael Cichocki May 29 '13 at 7:07

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.

share|improve this answer

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.

share|improve this answer
    
Got it..thanks :) –  joey rohan Apr 11 '13 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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