CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: Singleton

  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Thumbs up Singleton

    Hi,
    I can achieve the same functionality as of singleton by creating a static object.
    So what is the advantage of using the singleton class or design pattern.
    Plz correct me if I am wrong.
    Thanks

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Singleton

    a static object is one possibility of implementing a Singleton. Another possibility would be to create a new object in the free store on the first access to the Singleton.

  3. #3
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Singleton

    I can achieve the same functionality as of singleton by creating a static object.
    No, you can't. Singlteon means you have only one object of a such type with all the advantages of it whereas a static object is the static object only. It is not a replacement for the singleton. You should read more about the singleton http://sourcemaking.com/design_patterns/singleton

  4. #4
    Join Date
    Jul 2007
    Posts
    249

    Re: Singleton

    Why,what extra advantage is there in Singleton

  5. #5
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Singleton

    Rajesh1978, Did you read the text on the link i've posted above?

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Singleton

    The point of the singleton pattern is supposedly to enforce only one instance of a particular class, but in practice it seems it's mostly used to wrap global state in a more attractive package. I'm generally not a fan of it.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Singleton

    It's essentially the same advantage over global state that full class objects have over data-only structures: It comes complete with methods to refine and manipulate that state, all wrapped up in a tidy package.

    Could you achieve the same thing without a singleton, just using free functions? Absolutely. But that might not give you as useful of an abstraction about what's happening.

  8. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Singleton

    I think that I have seen what the OP is talking about. I have seen a pattern where all data members of the class are static. Even though you can create multiple instances, every instances reads and writes to the same class attributes, which are all static. In this case, the constructors are still public as are the member functions. Member functions are also non-static. Of course you still have to implement mutexes to protect the data in the case of multiple threads creating other instances. In some cases I have seen this done where all of the member functions are also static. In that case no instance is ever created.

    The main problem I have with this is that it isn't a singleton by definition since there are multiple instances but the design realizes similar goals. It actually works and I have had a hard time criticizing it. The main problem is that static member functions can't be virtual so it is impossible to extend the pattern using inheritance. On the other hand, in most cases that I have seen inheritance wasn't really needed so the designer didn't care. I'm not sure if this is what the OP is talking about. In the past I've seen these and disliked them but didn't have a real good argument for insisting that they be designed into real singleton's at the time.

    I've come to dislike singleton honestly. I think it is an overused pattern and for such a simple concept there seem to be a lot of complex issues associated with guaranteeing that only one instance can ever exist. There are plenty of other options for providing access to objects without using the singleton pattern and in a lot of cases there really isn't any danger of multiple objects being created unecessarily if factories and delegators are coded correctly.

  9. #9
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Singleton

    Quote Originally Posted by Richard.J View Post
    a static object is one possibility of implementing a Singleton. Another possibility would be to create a new object in the free store on the first access to the Singleton.
    To be honest, I am not sure what is meant by a static object. If the class declaration contains a public constructor then it isn't a singleton. The purpose of the singleton pattern is to "enforce" one and only instance of a type while providing a convenient way of accessing that one and only instance. If you have a type with a public constructor and you just create a static instance, there is nothing being enforced that would allow you to call that pattern a real implementation of singleton.

  10. #10
    Join Date
    Aug 2007
    Posts
    858

    Re: Singleton

    Quote Originally Posted by Lindley View Post
    It's essentially the same advantage over global state that full class objects have over data-only structures: It comes complete with methods to refine and manipulate that state, all wrapped up in a tidy package.
    I don't disagree with that aspect of it. What get me is, you poll programmers about global state, and most will say it's a bad thing. Poll them again about singletons, and most tend to say that they're good. What's the difference?

    There are certainly ways to get the encapsulation aspect and the single-instance aspect without having the global state aspect.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Singleton

    Quote Originally Posted by kempofighter
    I think that I have seen what the OP is talking about. I have seen a pattern where all data members of the class are static. Even though you can create multiple instances, every instances reads and writes to the same class attributes, which are all static.
    Heheh, I decided to check my copy of the GoF book, and yeah, that pattern is listed as one of the things that the singleton pattern can replace (and conversely, the singleton pattern is one of those things that that pattern can replace).

    Quote Originally Posted by kempofighter
    If you have a type with a public constructor and you just create a static instance, there is nothing being enforced that would allow you to call that pattern a real implementation of singleton.
    Yes, though I'd say that you can still refer to that lone object as a singleton, literally speaking.

    Quote Originally Posted by Speedo
    What get me is, you poll programmers about global state, and most will say it's a bad thing. Poll them again about singletons, and most tend to say that they're good. What's the difference?
    I would say that "most tend to say that they're good" is an exaggeration, but my take is that the difference is because in those specific cases where one really needs to enforce the singleton property and a single point of access makes sense, the singleton pattern is tolerable (but woe to you if your requirements change too much!), whereas ordinary global variables give you global state with no mitigating factors (other than allowing you to be careless about design).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Singleton

    The primary problem with global data, as I see it, is that it's uncontrolled and makes thread-safety hard.

    Singletons solve the second problem well enough. As for the first, that just requires careful design. Using a singleton helps to enforce the fact that you aren't just arbitrarily changing data; you're altering state which is designed and expected to be queryable in the first place.

  13. #13
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Singleton

    a class with all static methods is not singleton it is a nothington that lacks polymorphism and a built-in mechanism for construction and destruction and hence you can make a hierarchy of classes when using nothington. in another point of view when using nothingnot there is no object orientation at all and in fact your class is a collection of some functions that is not good as perspective of design. this issue that constructor and destructor are called automatically when using singleton is a very important issue rather than using nothington.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  14. #14
    Join Date
    Aug 2007
    Posts
    858

    Re: Singleton

    Quote Originally Posted by laserlight View Post
    I would say that "most tend to say that they're good" is an exaggeration, but my take is that the difference is because in those specific cases where one really needs to enforce the singleton property and a single point of access makes sense, the singleton pattern is tolerable (but woe to you if your requirements change too much!), whereas ordinary global variables give you global state with no mitigating factors (other than allowing you to be careless about design).
    Of course that's true in theory, but in practice it seems that singletons are a favorite point of abuse.

    The primary problem with global data, as I see it, is that it's uncontrolled and makes thread-safety hard.
    It also hides hides dependancies, which can make testing and reusing code a royal PITA.

  15. #15
    Join Date
    Mar 2009
    Posts
    51

    Re: Singleton

    Quote Originally Posted by Speedo View Post
    What get me is, you poll programmers about global state, and most will say it's a bad thing. Poll them again about singletons, and most tend to say that they're good. What's the difference?
    One difference is that 'global data' usually was lumped together in one big interdependent mess, while singletons can be used on their own. If a class uses a number of singletons, those singletons can be reused along with that class.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured