Singleton pattern in D

Daniel Keep daniel.keep.lists at gmail.com
Fri May 11 21:08:56 PDT 2007



Bill Baxter wrote:
> David Ferenczi wrote:
>>> Final point: just make sure you actually need singleton.  I've seen a
>>> lot of people using singleton for a single class, which would be more
>>> efficiently handled by a static class.
>>
>> Can you mention some godd and bad examples?
>>
>> I use singleton pattern e.g. for the application class (command line
>> interface), configuration class (configuration handling), an log class
>> (logging).
> 
> Good ole Yegge comes through again here with another beautiful rant:
>   http://steve.yegge.googlepages.com/singleton-considered-stupid

In my *defense* 'yer honah, I was using singleton instances for the
rendering and input subsystems for a game, of which there was only one
linked into the executable.  The reason I did all that was because I
wanted to be able to say

  engine.gx.Gx.doStuff()

Instead of:

  engine.gx_sdl_gl20.gx.Gx.doStuff()

Sadly, D doesn't have runtime class variables, so I kinda had to fake it
:P  I could have used an alias, but that would have, again, involved
actually referring to the concrete class at some point.  All this
because I wanted to be able to pull out the renderer and change it
without having to touch a line of code anywhere else.

I swear, one of these days I'll stop doing all this crazy stuff just to
avoid changing a few lines of code :P

... naaah.

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/


More information about the Digitalmars-d-learn mailing list