Alex
Alex

Reputation: 44265

When is the Singleton pattern important?

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?

Upvotes: 1

Views: 1660

Answers (5)

Jeroen De Dauw
Jeroen De Dauw

Reputation: 10908

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/

Upvotes: 0

Ben
Ben

Reputation: 2388

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.

Upvotes: 0

SomeWittyUsername
SomeWittyUsername

Reputation: 18338

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.

Upvotes: 0

George Barbulescu
George Barbulescu

Reputation: 131

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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.

Upvotes: 1

Related Questions