[rfc][icedtea-web] immutbale transaltor

Jiri Vanek jvanek at redhat.com
Fri Sep 19 13:34:53 UTC 2014

On 09/19/2014 03:09 PM, Jie Kang wrote:
> Hello,
> I think this patch is fine. Okay for me :)
> On a side note:
> I think at this point you can ditch the singleton pattern. I am pretty sure the existence of non-private constructors, even protected ones, invalidates the singleton pattern but this is kind of semantics. I mean, you could say a class with public constructors that's only ever instantiated once is a singleton then. I think an important concept to the singleton pattern is the compile-time guarantee that no instances outside of your singleton(s) will ever get instantiated.
> Also, the singleton pattern by design is untestable, however the choice of use is cost-benefit. You gain the benefits of single instance with a compile-time guarantee among other things, with the costs of untestability, inability to subclass, etc. I think in general, you should really question any singleton use and try to avoid it as much as possible. If you want a testable singleton, it just doesn't really exist.

I'm not sure how to feel about that. Partially I agree, And I already once experienced, that another 
programmer was happily instantiate singletons, simply because he could. And that flaw was found 
really late.

So from this point, the enum is much MUCH better.

However, this can be solved by private constructor. And imho even our case canbe transformed to this 
more correct case. Do you wont to as exercise? :)

By this trick* the enum lost all its disadvantages.

Another way how to achive this is to heve also getInstance private, and have only static api...

> For why people use enums when they choose to create a singleton, it's really because it is simple and provides pretty much everything a singleton needs. A guarantee that there is only one instance, the ability to serialize without extra work, what's not to like? Apart from the fact that it's a singleton I guess.

* trick - yes - because you need to bent the testable instances classes to workaround it.

Is the enum really so protective? Imho not - on level of bytecode you can manipualte it and all 
those check you wont from it are gone....

> I feel like you mean to ask, why do people use singletons, instead of why do people use enums for singletons :D

Oh definitely not. Acctually I'm big fan of singletons. And I prefer theirs usages over  static 
where possible.
> Regards,

Sumamrised my inner thoughts  - to be able to test is more necessary then compile time check on 

How is this achived, and checked  ..well impelmented . is different issue  ... - more social 
engineering then artificial one.


More information about the distro-pkg-dev mailing list