Re: Singleton Pattern



Jon Harrop wrote:
theinvisibleGhost wrote:

I've just started working on a very large project
that uses the singleton pattern to create just about everything.
Argghh!
Why did someone ever allow this pattern to come into existence.
It just seems an incredibly posh way of using global data!


The overuse of singletons doesn't really have anything to do with OO, it is
a consequence of most people programming in OO languages than lack better
alternatives (e.g. modules, nesting).

I think it does have to do with OO. It's really taking the easy way out. When you are tempted to use a global variable and you decide to do it, one thing that you are doing is shutting off some feedback that can help your design. When you skip globals and start to pass variables around, you alter your design so that you don't have to do that as often. What you end up with is more layering and separation of responsibility.


Databases are a typical example. If your database access is global and you use it every place (easy to do) you essentially have no layering. If you pass around references to it's access, it isn't too long before you discover that not everything needs the database and your design stratifies as a result.

Now, there's a big caveat here. Some people, if asked not to use globals, would pass variables through argument lists over and over again and not do anything to the design to make things easier. But, that's another problem entirely. For people who care about the structure of their code and its ease of modification, avoiding globals gives you more feedback as you develop and it helps you arrive at a better design.


Michael Feathers author, Working Effectively with Legacy Code (Prentice Hall 2005) www.objectmentor.com .



Relevant Pages

  • Re: Scope of Database Definition
    ... In the early days of Access, some developers used globals because: ... It used less resources to keep reusing the same database variable. ... set GblDb = CurrentDb ...
    (microsoft.public.access.tablesdbdesign)
  • Re: [PHP] uh oh, I defined a resoruce
    ... I don't agree that "Global variables are evil." ... going to use only for a database connection, IMO, is perfectly fine. ... By specific globals I mean that I have a "namespaced" entry such as ...
    (php.general)
  • Re: [PHP] uh oh, I defined a resoruce
    ... I don't agree that "Global variables are evil." ... going to use only for a database connection, IMO, is perfectly fine. ... By specific globals I mean that I have a "namespaced" entry such as ...
    (php.general)
  • Re: defvar affecting captured closure variables ?
    ... but global specials used to be the DEFAULT ... those of us on the CLTL design team basically said something very close ... the *foo* naming convention was not widely used in most ... bind these globals, since otherwise you'll get a lexical foo when you ...
    (comp.lang.lisp)
  • Re: Classes / Functions / Autonomy
    ... and they will never interfere with the rest of the script. ... Of course when I first started out, I was using globals. ... Hrm, Im thinking of things like Database Name, Highlight Colours, I even went anal at one point and stored a URL that would retrieve info from a database. ... var bgColor; ...
    (comp.lang.php)