home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 31,706 of 33,346   
   Henning Follmann to Trevor Taylor   
   Re: Regarding Design Pattern   
   29 Nov 11 22:31:53   
   
   142a0ddb   
   From: hfollmann@itcfollmann.com   
      
   On 2011-11-28, Trevor Taylor  wrote:   
   > If you read Design Patterns (Gamma et al), I'd suggest skipping the   
   > Singleton section.   
   >   
   > In my experience, the second part of the first sentence of this   
   > section has taken software engineering *backwards*.   
   >   
   > The first sentence starts out well enough:   
   >   
   > "Ensure a class only has one instance"   
   >   
   > ... and then unfortunately adds:   
   >   
   > "and provide a global point of access to it."   
   >   
   > That last bit has just described a global variable, which the computer   
   > science community decided was a bad idea back in the 1970s (well,   
   > that's how far my memory goes back).   
   >   
      
   You mix things up here.   
   A global variable is a "code organization/accessibility" concept. That is   
   scope.   
   Bad about this is the visibility of the _name_ of a variable, what potentially   
   can lead to multiple declarations.   
   Just think of "i" as the most common used index in loops. If this a global   
   scoped variable then you would be in trouble if a loop in a loop uses   
   this global "i" in both places.   
      
   But a singleton most likely is a container fo some information, which will be   
   used in more than just one place. So you have a single source of information   
   it has to be globally accessible.   
      
      
   > I can vividly recall 4 multi-year projects I've worked on where the   
   > singleton term was used to explain use of global variables, and the   
   > authors agreed that global variables were bad but argued that they   
   > were using singletons not global variables.   
   >   
      
   after 4 years you should know the difference between scope and access.   
   and so should your team mates.   
      
   > Please excuse me for humbly offering my tips for not using global   
   > variables (including not using singletons as global variables) - which   
   > is borderline off topic:   
      
   no, singletons are great. Hold your config informations in a singleton.   
   That would be a propiate use for a singleton.   
      
      
   >   
   > - organise your software into a strict heirarchy of objects/classes,   
   > ie directed dependency graph, no cycles   
   > - where object A needs to use object B, explicitly give A a reference   
   > to B (in C++, pass the reference to A's constructor)   
      
   That wont work when during your construction you don't have a reference,   
   will it?   
      
      
   { Unnecessary trailing quotation removed -mod }   
      
   --   
   Henning Follmann           | hfollmann@itcfollmann.com   
      
      
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca