1

In coding, there exists a Pattern called Singleton, restricting the instantiation of a class to only one single object. But what are the reasons to use a Singleton, if one can just instantiate the class once on module level, and e.g. name it with an underscore and use just that? Why and when should a Singleton be used?

To be specific (as no answer really is satisfying for me) here is a concrete example. I specify a standard python class and create an instance:

class MyObject(object):
    ....

_myinstance = MyObject()

Besides the fact that this implementation does not prevent the creation of a second instance, under what circumstances is the use of a Singleton better than this simple implementation?

1

5 Answers 5

1

The Singleton pattern is useful when you want to reuse some expensive to create resource. Since the creation is performed only once for the entire lifetime of the application, you are paying the price only once.

3
  • But for this case I also can create a single instance of the expensive class in the module, without the need to use a Singleton. Maybe I do not understand?
    – Alex
    Dec 10, 2012 at 6:47
  • @Alex if this module belongs only to you, and noone else have access to the source code of it, then yes. Anyway forcing this class to be created only once would be a better choice.
    – dantuch
    Dec 10, 2012 at 6:55
  • @Alex, yes, exactly, you could create a single instance of the resource for this case => that's exactly what the singleton pattern is :-) That's the name of the pattern you would be using. Dec 10, 2012 at 6:59
1

Singleton pattern is useful when a resource is expensive in terms of occupied memory or time that it takes to be created. Another usage that can be found to the singleton pattern is that you can restrict the number of instances of a class base on you application requirements, one example that comes in mind is the connection to a database, where you will always make one connection per thread ,that connection being usually alive along with thread.

0

Singleton is essentially a concept stating that some entity must have only single instance. The reasons for single instances can vary, these are the main ones:

  • Reuse a shareable resource in different modules.
  • Disallow multiple instances of a resource.
  • Create an instance when any using module isn't ready yet - e.g., a global variable that is created and initialized before any program code runs, so when the using modules are created, the global is ready.
0

The reasons for using a singleton depend on what the alternative is.

Compared to an object that is explicitly created at one point in the client code, a singleton is easier to access from multiple classes, packages, modules etc. This has advantages but can encourage the creation of overly tightly coupled code. The explicit approach makes the dependency clearer but can be hard to implement, particularly when building on existing code.

The alternative to a singleton that I have more often seen in practice is the "class" that is actually a collection of quasi-global variables. Compared to this a singleton (or any object) is a great improvement as it limits coupling across different parts of the code, and is easier to test or to replace with a stub or mock when testing other classes. And it makes it much easier to modify your code later to allow more than one instance: in many cases the requirement that there should be only one instance is not really that strong, and this flexibility can be useful.

When refactoring existing code that uses a bunch of global variables, a singleton can be an achievable first step that can then be refactored further into an explicitly created object if this is needed and is worth the extra effort.

0

Since people here have already done a good job in highlighting why you would want to use a Singleton, I'd like to highlight that there are reasons why you perhaps should not use it. A good blog post I came across recently on this topic: http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/

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.