Jump to content

  • Log In with Google      Sign In   
  • Create Account


static ... bad design ?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Alundra   Members   -  Reputation: 825

Like
0Likes
Like

Posted 17 February 2013 - 06:57 PM

Hi,

using a singleton need to check if the instance is allocated.That demand this check eveytime we need to use the singleton.

The alternative is to use a static but recently i have listen that using static is not good because of multithreading.

Using static is a bad design ?

 

Thanks



Sponsor:

#2 Hodgman   Moderators   -  Reputation: 29248

Like
8Likes
Like

Posted 17 February 2013 - 07:04 PM

Static/global/singleton are all basically the same idea, and have the same downsides.

 

using a singleton need to check if the instance is allocated.That demand this check eveytime we need to use the singleton.
The alternative is to use a static

It's the same thing, e.g.

Foo& GetFoo()
{
  static Foo foo;
  return foo;
}
If you look at the assembly code that is generated from the above code, the way that it's implemented actually looks like:

Foo& GetFoo()
{
  static bool initialized = false;
  static byte foo[sizeof(Foo)];
  if( !initialized )
  {
    initialized = true;
    new(foo) Foo();
  }
  return *(Foo*)foo;
}


#3 ApochPiQ   Moderators   -  Reputation: 14886

Like
6Likes
Like

Posted 17 February 2013 - 07:05 PM

Singletons, static members, and global variables are all the same thing wearing different trousers.

 

The goodness of the design is not inherent to the tool, it's about how you use it. Static members have their place in certain circumstances, as do globals. (I leave out singletons on purpose, but we've had that argument a million times and I will not have it repeated here.)

 

You haven't really described why you think this is a necessary design decision, but based on sheer probability and the fact that you mentioned a singleton to begin with, I'd wager decent money that your specific use case is "bad" design.



#4 LorenzoGatti   Crossbones+   -  Reputation: 2659

Like
0Likes
Like

Posted 18 February 2013 - 04:52 AM

Hi,using a singleton need to check if the instance is allocated.That demand this check eveytime we need to use the singleton.
The alternative is to use a static but recently i have listen that using static is not good because of multithreading.

You haven't really described why you think this is a necessary design decision, but based on sheer probability and the fact that you mentioned a singleton to begin with, I'd wager decent money that your specific use case is "bad" design.

The bad design is, obviously, having to check on access whether something has been initialized: it would be equally bad for non-singleton objects. The actual alternative is not between a singleton and a static variable, but between silly "on demand" initialization (like in Hodgman's code snippet) and a correct object lifetime (ensuring that static variables are initialized before access, like in Java where the first reference to a class pauses client code to load the class and run its static initialization).


Produci, consuma, crepa

#5 rip-off   Moderators   -  Reputation: 8107

Like
3Likes
Like

Posted 18 February 2013 - 10:35 AM

Using any kind of shared mutable state is not going to play nice with multi-threading. Even if you fix initialisation problems, you still need to deal with the thread safety when accessing and modifying the state itself.

 

This is regardless of whether you expose such state as a global, a static value, a singleton, or even passing a reference.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS