Archived

This topic is now archived and is closed to further replies.

eldee

Singletons

Recommended Posts

What is the benefit of using a singleton pattern vs. one global instance of a class? sure, you have protection against accidentally creating a new instance, but is that it? i''m aware that 9 times out of 10 you wont need a singleton, and i''m guessing that my current situation is one of those times i''ll pass. I have a timer class that is conflicting delay counts due to GetTickCount. the solution i''ve decided on, is to create a global "clock" class that will update all of the other timer instances. so i guess my question is this: when do YOU use singletons, and why? -eldee ;another space monkey; [ Forced Evolution Studios ]

::evolve:: Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

Share this post


Link to post
Share on other sites
1. The first major benefit is being able to access that one and only one instance of the class through an interface method across multiple source files without the use of externs everywhere. Externs are messy and make things hard to maintain and should be avoided at all costs.

2. You can have more control over the creation and destruction of a singleton.

An alternative is a static class where all the members in a class are static (both member functions and data members). In terms of class interface these are not quite as clean as singletons but require a lot less indirection. Only two levels of indirection for static classes but normally with a singleton you will require three. Statics are also could for callbacks as you can resolve the address of a static member function and with a singleton this is not easy.

If you dont need callbacks and are absolutely sure only one instance of a class is required and you require some control over the creation and destruction of that instance then go with a singleton. If you can't be bothered with three levels of indirection and need a little extra performance in debug modes then your probably better of with a static class. A good example of a static class is a Render class and a Timer class but singletons are just as good a solution. It does also come down to personal preference. People go a little crazy with design patterns, the most important thing to remember is that singletons, factories, static classes etc are just tools it's a question of picking the right tool for the job.



[edited by - level10boy on January 20, 2003 8:47:30 PM]

Share this post


Link to post
Share on other sites
IMHO, if there''s few related (global) objects that must be instantiated (and got destructed) in an orderly fashion to work, but only an instance of these is needed... however to achieve it require some ''manual work''... why not group them together and say *gulp* singleton?

Singletons are not just soup of singly global objects.

quote:

I have a timer class that is conflicting delay counts due to
GetTickCount. the solution i''ve decided on, is to create a global
"clock" class that will update all of the other timer instances.


I think you don''t need singleton for these.

Share this post


Link to post
Share on other sites