Skip to main content
Edit #2: From Herb Sutter on Singletons
Source Link
red-dirt
  • 3.6k
  • 1
  • 24
  • 27

Without examining that thread, I can tell you I've seen it used numerous times as just a thin wrapper around a global variable. It's akin to putting lipstick on a pig... Just because you've wrapped it in a design pattern, doesn't make it OK.

edit: Apparently I need to explain why globals are bad? You can read about globals here if you need an intro.

"Scribes frequently use Singletons in a misguided attempt to replace global variables. I have, for example, been on projects where the Singleton has been described as 'a well-known object' -- does that not sound like a global variable, my child?"

"Umm... no, not really," I drawled after a bit of consideration. I wrote on my whiteboard:

S &S::Instance() 
{   
    static S theInstance;   
    return theInstance; 
}

"I don't see a global variable here." "Ah, my child, but there is a global variable -- a global variable named S::Instance(). Consider this parable:

class T { /* whatever */ }; 
T globalT;

"What is the difference between globalT and theInstance? Both variables have static storage duration, do they not?"

Therefore, never create a Singleton whose sole purpose is to mask a global variable.

Without examining that thread, I can tell you I've seen it used numerous times as just a thin wrapper around a global variable. It's akin to putting lipstick on a pig... Just because you've wrapped it in a design pattern, doesn't make it OK.

edit: Apparently I need to explain why globals are bad? You can read about globals here if you need an intro.

Without examining that thread, I can tell you I've seen it used numerous times as just a thin wrapper around a global variable. It's akin to putting lipstick on a pig... Just because you've wrapped it in a design pattern, doesn't make it OK.

edit: Apparently I need to explain why globals are bad? You can read about globals here if you need an intro.

"Scribes frequently use Singletons in a misguided attempt to replace global variables. I have, for example, been on projects where the Singleton has been described as 'a well-known object' -- does that not sound like a global variable, my child?"

"Umm... no, not really," I drawled after a bit of consideration. I wrote on my whiteboard:

S &S::Instance() 
{   
    static S theInstance;   
    return theInstance; 
}

"I don't see a global variable here." "Ah, my child, but there is a global variable -- a global variable named S::Instance(). Consider this parable:

class T { /* whatever */ }; 
T globalT;

"What is the difference between globalT and theInstance? Both variables have static storage duration, do they not?"

Therefore, never create a Singleton whose sole purpose is to mask a global variable.

added 178 characters in body
Source Link
red-dirt
  • 3.6k
  • 1
  • 24
  • 27

Without examining that thread, I can tell you I've seen it used numerous times as just a thin wrapper around a global variable. It's akin to putting lipstick on a pig... Just because you've wrapped it in a design pattern, doesn't make it OK.

edit: Apparently I need to explain why globals are bad? You can read about globals here if you need an intro.

Without examining that thread, I can tell you I've seen it used numerous times as just a thin wrapper around a global variable. It's akin to putting lipstick on a pig... Just because you've wrapped it in a design pattern, doesn't make it OK.

Without examining that thread, I can tell you I've seen it used numerous times as just a thin wrapper around a global variable. It's akin to putting lipstick on a pig... Just because you've wrapped it in a design pattern, doesn't make it OK.

edit: Apparently I need to explain why globals are bad? You can read about globals here if you need an intro.

Source Link
red-dirt
  • 3.6k
  • 1
  • 24
  • 27

Without examining that thread, I can tell you I've seen it used numerous times as just a thin wrapper around a global variable. It's akin to putting lipstick on a pig... Just because you've wrapped it in a design pattern, doesn't make it OK.