On Tuesday, March 06, 2018 11:52:05 H. S. Teoh via Digitalmars-d-learn 
wrote:
> On Tue, Mar 06, 2018 at 11:46:04AM -0700, Jonathan M Davis via 
Digitalmars-d-learn wrote:
> > On Tuesday, March 06, 2018 18:34:34 bauss via Digitalmars-d-learn wrote:
> [...]
>
> > > Singletons are always smelly code tbh.
> > >
> > > Especially in D with thread-local storage.
> > >
> > > I can't think of a situation where you truly need singletons in D.
> >
> > I confess that I've never really understood why some folks dislike
> > singletons so much, but then again, I've only rarely found cases where
> > I thought that they made sense, and I gather that some folks out there
> > use the singleton pattern way too much (I've heard it suggested that
> > it's because it was one of the few design patterns from the design
> > pattern book that was easy).
>
> [...]
>
> To me, a singleton is a symptom of excessive dogmatic adherence to the
> OO religion where Everything Must Be A Class No Matter What. When there
> can only be one of something, it's clearly no longer a *class*, but is
> either a module, a global variable, or a (set of) global function(s).
>
> I'm curious to know what are the few cases where you think a singleton
> made sense, where it wouldn't be more appropriately implemented as a
> module, global variable, or global function(s).

It makes sense whenever it doesn't make sense to have multiple instances of
a particular class, and it makes sense to enforce that. As I explained,
LocalTime and UTC from std.datetime do that for exactly that reason. Cases
like that are rare, but they do exist. However, they also exist rarely
enough that it's going to be hard to come up with more examples.

I judge whether singletons makes sense on a case-by-case basis. Sometimes,
they do make sense, so I wouldn't say that they're necessarily a code smell,
but I also completely agree that they rarely make sense. So, I don't
disagree that singletons should rarely be used. I just disagree with the
idea that they should never be used.

And for better or worse, singletons are one of those things that seems to me
like it should usually be obvious whether they make sense in a particular
case, but that doesn't seem to be true for everyone, otherwise there
wouldn't be as much of a dislike of singletons as there seems to be, since
they wouldn't come up very often.

- Jonathan M Davis

Reply via email to