The singleton design pattern in D, C++ and Java

Bruno Medeiros brunodomedeiros+spam at com.gmail
Fri Aug 6 14:43:18 PDT 2010


On 05/08/2010 17:18, Andrei Alexandrescu wrote:
> Mike James wrote:
>> "Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message
>> news:i39d0s$2596$1 at digitalmars.com...
>>> Bruno Medeiros wrote:
>>>> On 16/07/2010 18:18, Ali Çehreli wrote:
>>>>> Justin Johansson wrote:
>>>>>> Which language out of C++, D and Java does this classical
>>>>>> "GoF" (gang-of-four***) design pattern best?
>>>>>
>>>>> Are we still talking singleton? I thought that it is considered an
>>>>> anti-pattern already. :)
>>>>>
>>>>
>>>> First time I've heard this as well. I searched the web, and woah,
>>>> there does seem to be quite a few people who think the same, but
>>>> frankly, upon reading their arguments against singleton (in the
>>>> sense that singleton should not be used), most of them don't add up.
>>>> In fact some of those arguments are quite idiotic, a lot of people
>>>> are totally misunderstanding singleton.
>>>> (im not going to botter arguing why, and I hope people here don't
>>>> think singleton is inherently bad)
>>>> Cause man, I haven't seen such idiocy since when I joined a new
>>>> company and found the then lead developer enforced a policy in our
>>>> Java codebase of single return statements. Because multiple return
>>>> statements were very bad and should *only* be used if performance
>>>> necessitated that. Lolz. Talk about software engineering best
>>>> practices from the 60's.
>>>
>>> OUCHHHHHHHH...
>>>
>>> Andrei
>>
>> Having a single point of exit from a function is still a design
>> practice in the Joint Strike Fighter (JSF) C++ Coding Standard (Rule
>> 113) and is a requirement in MISRA C++:2008 (Rule 6-6-5) and MISRA
>> C:2004 (Rule 14.4) and also a requirement to writing software under
>> IEC61508 (Part 3, Table B.9). So its still an engineering best
>> practice in the 10's :-)
>
> I know! And that doesn't help either!
>
> With exceptions, SESE effectively becomes a dangerous illusion.
>
>
> Andrei

My thoughts exactly. Although its not the only reason for it being outdated.

Going back to the previous post, well, yes, if you are coding in C or 
some similar pre-OO, strutured programming language (in other words, 
languages from the 60s and 70s :p) it still makes *some* sense to follow 
that rule. The context and principles for the rule are still valid. 
Perhaps also for C++ if you restrict yourself to just a subset of C++ 
functionality. Like in the case of the JSF C++ coding standard, which 
also says:
"C++ exceptions shall not be used (i.e. throw, catch and try shall not 
be used.)" !

But for most, if not all, newer-than-c mainstream languages, its just a 
completely bonkers idea, and not just because of exceptions.
Anyways, moving on...

-- 
Bruno Medeiros - Software Engineer


More information about the Digitalmars-d mailing list