   singleton or global?
  Now I know what a singleton is, but I don't see the what the big deal is. It's just a dressed-up global variable. You can more or less control when it's created and destroyed, but its lifetime is usually from the beginning to the end of the app anyway. What TANGIBLE benefit does it give you over a traditional global variable?The way I see it, if you have enough discipline to modify/hack your class into a singleton, then you have enough discipline to use one and only one gobal instance of said class. Not to mention that IF you have the need for more than one instance of the class later on, then it's a lot simpler to accomodate that using globals than if you used a singleton. One example is an error logger. Most people use singletons for those, but I find it quite useful to output to more than one file, so I just instantiate more than one object (they don't even have to be global either). That wouldn't be possible with singletons...Help me "see the light" ...[edited by - foofightr on June 29, 2003 4:42:12 AM]
quote:Original post by foofightr  What TANGIBLE benefit does it give you over a traditional global variable?None. Singletons are a sad side effect of academia invading programming. That should be enough to turn on the flame tossers. ;  Someone here pointed out that singleton is a "design shield". It suddenly makes globals all OK and universally accepted thing, since it''s got a cool new name and it''s described in the holy GOF (="Design Patterns&quot book.Here''s "the light": Using singletons is just cheating on yourself. Unless you''re trying to make yourself look good for your boss or something.Oh, but traditional globals are evil too.. ;quote:Original post by foofightr  Most people use singletons for those, but I find it quite useful to output to more than one file, so I just instantiate more than one object (they don''t even have to be global either). That wouldn''t be possible with singletons...Actually, you can extend the design of a singleton to allow for the controlled instantiation of any arbitrary set number of objects.;quote:Original post by foofightr  Most people use singletons for those, but I find it quite useful to output to more than one file, so I just instantiate more than one object (they don''t even have to be global either). That wouldn''t be possible with singletons...Actually, you can extend the design of a singleton to allow for the controlled instantiation of any arbitrary set number of objects. your mom''s on my buddy list...Mage ;  I don''t want flames or anything, I am genuinely interested in what a singleton has to offer. Gotta be something I''m missing. A lot of people use them, but I wonder what for... ;  Here an interesting article about it http://www.cuj.com/documents/s=8232/cuj0303hyslop/ ;  I am using something what I am calling SemiSingletons (slightly modified singleton architecture). And it has several features than global variable design.- a library with singleton is more "design safe" (I do not want to tell you that it is impossible with global variable design. But with global variable design it is IMO more difficult and proprietary). It means that bad use of library is banned from source code so it is not possible to compile it.- you have general and INNATE control on semisingeton creation/removing order- it is more elegant ;  you can only have one singletonlook at this:extern CSystem g_System;you can still write void func() {  CSystem local_System;}a singleton is really only one object, you can never create more than one of it.there will be only one CSystem in your whole application then.not that i think it will ever be useful.   "take a look around" - limp bizkitwww.google.com ;quote:you can still writevoid func() {CSystem local_System;}Sure, you can write this too:void f()<%int*p;while(1)p=new int[10000];%>  Edit: Obfuscation.[edited by - Leffe on June 29, 2003 6:24:41 AM]