Traditional Singleton Implementation vs Static Object Singleton Implementation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Traditional Singleton Implementation vs Static Object Singleton Implementation

  1. #1
    Join Date
    Apr 2011
    Posts
    2

    Question 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

  2. #2
    Join Date
    Jul 2002
    Posts
    2,494

    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...

  3. #3
    Join Date
    Nov 2003
    Posts
    1,778

    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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Traditional Singleton Implementation vs Static Object Singleton Implementation

    Quote Originally Posted by Codeplug View Post
    #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.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,077

    Re: Traditional Singleton Implementation vs Static Object Singleton Implementation

    Quote Originally Posted by Lindley View Post
    Is there documentation on this? I hadn't heard it before.
    the draft in my possession reads

    Quote 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.

  6. #6
    Join Date
    Apr 2011
    Posts
    2

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center