-
April 7th, 2011, 06:43 AM
#1
Traditional Singleton Implementation vs Static Object Singleton Implementation
I have usually implemented Singletons as described in the Design Patterns book. Recently someone suggested another implementation of Singleton using creating a static instance of a class and returning that instead of using new and returning a pointer to the new singleton. I would like to get your input. Is there any advantages of using one or the other? Disadvantages? Disadvantages for the static singleton implementation?
Let me illustrate:
Traditional Singleton Implementation
Code:
Car* Car::getInstance()
{
if(!InstanceFlag)
{
Instance = new Car();
InstanceFlag = true;
return Instance;
}
else
{
return mp_Instance;
}
}
Static Implementation
Code:
Car& Car::getInstance()
{
static Car theCar;
return theCar;
}
Advantages & Disadvantages? Any documents or articles I can refer to for the second implementation ? Thanks for your help!
Last edited by ovidiucucu; April 7th, 2011 at 07:51 AM.
Reason: added [CODE] tags
-
April 7th, 2011, 06:51 AM
#2
Re: Traditional Singleton Implementation vs Static Object Singleton Implementation
Looking only at provided code, I can say that:
1. First version is not thread-safe.
2. In the second version Car instance is created even if getInstance is never called. Not optimal, especially if the car is expensive...
-
April 7th, 2011, 09:57 AM
#3
Re: Traditional Singleton Implementation vs Static Object Singleton Implementation
#2 is also not thread-safe in C++03. It is in C++0x
In #2, storage space is pre-allocated for Car, but the constructor is only called at the first call to getInstance().
gg
-
April 7th, 2011, 10:25 AM
#4
Re: Traditional Singleton Implementation vs Static Object Singleton Implementation
Originally Posted by Codeplug
#2 is also not thread-safe in C++03. It is in C++0x
Is there documentation on this? I hadn't heard it before.
-
April 7th, 2011, 11:37 AM
#5
Re: Traditional Singleton Implementation vs Static Object Singleton Implementation
Originally Posted by Lindley
Is there documentation on this? I hadn't heard it before.
the draft in my possession reads
Originally Posted by 6.7.4 Declaration statement
The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or [...] Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization [...] If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.
-
April 7th, 2011, 08:19 PM
#6
Re: Traditional Singleton Implementation vs Static Object Singleton Implementation
Alex, what do you mean in #2 an instance of the Car is created even if getInstance is not called? Can you explain?
Codeplug, for #2 memory is preallocated and if you were running out of memory you would know at build time instead of run time like with dynamic memory allocation I ran into issue trashing the heap. In that case #2 might be better.
Lindley, Im have not found documentation on this way of implementing a Singleton (#2) and that is why I decided to post this topic.
Thank you all for replying. I'm still trying to figure it out whey #1 may be better than #2 or viceversa. Keep in mind that #1 is the solution suggested by the Design Patterns book and what I have seen in the industry. Thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|