• Advertisement
Sign in to follow this  

The Singleton Pattern: To be or not to be [used]?

This topic is 1896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

You want just one: Instanciate just one. You want global access? Make it a global.

This is the right spirit. Avoiding useless extra instances of objects and getting the useful objects where are needed are two interlinked rather difficult problems; rather tragically, progressing on the former makes the latter harder.
Good solutions require good design and forethought, and often tedious code, for example lots of parameters and variables to pass objects around or checks of the presence and validity of objects that might or might not have been initialized, not to mention (in many cases) understanding and accepting the price of global variables: obviously the average mediocre programmer is attracted by singletons because

  • Singletons look clever and legitimate (a tie-wearing design pattern!) without entailing actual design effort. If it shouldn't have been a singleton, you'll find out when it's too late to change your mind cheaply, because you didn't consider different options and contingencies at the proper time.
  • Accessing singletons allows a strong subconscious denial (or intentional camouflage) that you are using a global variable, and a delusion that what you are doing is technically better (or at least more enterprisey).
    Edited by LorenzoGatti

Share this post


Link to post
Share on other sites
Advertisement
enterprisey


Increasing visibility is a good thing, and it will surely make us more... visible. To do that we should, uh... develop our strategy and strategize our development. Implement solutions and solutionize implementations. Aggressively.

Jake1.gif

Edgy?

Share this post


Link to post
Share on other sites

[quote name='davepermen' timestamp='1352645783' post='4999916']You want just one: Instanciate just one. You want global access? Make it a global.

This is the right spirit. Avoiding useless extra instances of objects and getting the useful objects where are needed are two interlinked rather difficult problems; rather tragically, progressing on the former makes the latter harder.
Good solutions require good design and forethought, and often tedious code, for example lots of parameters and variables to pass objects around or checks of the presence and validity of objects that might or might not have been initialized, not to mention (in many cases) understanding and accepting the price of global variables: obviously the average mediocre programmer is attracted by singletons because

  • Singletons look clever and legitimate (a tie-wearing design pattern!) without entailing actual design effort. If it shouldn't have been a singleton, you'll find out when it's too late to change your mind cheaply, because you didn't think consider different options and contingencies at the proper time.
  • Accessing singletons allows a strong subconscious denial (or intentional camouflage) that you are using a global variable, and a delusion that what you are doing is technically better (or at least more enterprisey).

    [/quote]

    What's the price of a global variable?

Share this post


Link to post
Share on other sites
What's the price of a global variable?


Loss of access control. (Also violation of dogma.... j/k)

Globals can be reached by parts of the program that have no business reaching them and can result in serious design problems later on. It's a matter of whether or not you're working with people that can't handle the responsibility. Sadly this is a lot of people, from what I hear. Once things get complicated it can become nearly impossible to have reasonable state expectations for something with global accessibility.

That's a good article you got there, Hodge. Edited by Khatharr

Share this post


Link to post
Share on other sites
This one is also worth reading: http://tech.puredanger.com/2007/07/03/pattern-hate-singleton/

Cool quote:
First, it is important to separate the “need for one of something in my system” from the “singleton pattern”. It’s perfectly reasonable to want one of something but to avoid using the singleton pattern. In other words, the problem is in the pattern, not in the goal.[/quote]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement