Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #37908

Re: Guice vs plain singleton

Path csiph.com!aioe.org!.POSTED!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Guice vs plain singleton
Date Sat, 23 Dec 2017 19:27:59 -0500
Organization Aioe.org NNTP Server
Lines 38
Message-ID <p1msag$m6p$1@gioia.aioe.org> (permalink)
References <p1e2jv$1jmk$1@gioia.aioe.org> <p1e4i8$3rc$1@newsreader4.netcologne.de> <p1gb9a$t9s$1@dont-email.me> <28fcb1d3-0fa4-48c0-a6f2-972e727cb874@googlegroups.com> <p1kafp$10rq$1@gioia.aioe.org> <p1lrv0$26c$1@news.albasani.net> <1f5b16b0-14fd-4725-a9f6-0d2b31bc7d5e@googlegroups.com>
NNTP-Posting-Host PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0
Content-Language en-US
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.lang.java.programmer:37908

Show key headers only | View raw


On 12/23/2017 12:54 PM, lewbloch@gmail.com wrote:
> While it's true that some inherited methods from `Enum` are
> superfluous for singletons, most of the argument against using enums
> for singletons derive from undocumented and idiosyncratic assumptions
> about what an enum "should" be. For example, citing enums'
> serialization mechanism as an contraindicator is a red herring.
> 
> The fact is that a hand-rolled singleton type requires great care to
> protect against things like serialization mistakes that enums solve
> for free. Given how badly every single Java candidate I've
> interviewed did at implementing singletons, I'd much rather have a
> few superfluous methods than a crappy implementation.
> 
> There's absolutely nothing in the idea of an enum to dissuade one
> from having only one enum constant, contrary to the argument
> presented. Yes, enum doesn't solve every possible problem, so you
> might occasionally want to roll your own. But the advantages of enums
> for singletons are so strong, and the risk of the custom approach is
> high enough, that enums should be the first and usually the last
> approach to consider.


It depends on what evaluation criteria is used.

If one want to minimize lines of code then it is a
fine solution.

If one want to write nice readable code following
OOP principles then it is a bad solution.

A singleton is not an enumeration and making it
one to save lines of code for making it thread safe
is not how one should write code. One should use
enum for types that are actual enumerations.

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Guice vs plain singleton Victor Porton <porton@narod.ru> - 2017-12-20 18:19 +0200
  Re: Guice vs plain singleton Patrick Roemer <sangamon@netcologne.de> - 2017-12-20 17:53 +0100
    Re: Guice vs plain singleton Daniele Futtorovic <da.futt.news@laposte.net> - 2017-12-21 13:59 +0100
      Re: Guice vs plain singleton lewbloch@gmail.com - 2017-12-21 11:36 -0800
        Re: Guice vs plain singleton Arne Vajhøj <arne@vajhoej.dk> - 2017-12-22 20:11 -0500
          Re: Guice vs plain singleton Sebastian <nowhere@nirvana.org> - 2017-12-23 16:15 +0100
            Re: Guice vs plain singleton lewbloch@gmail.com - 2017-12-23 09:54 -0800
              Re: Guice vs plain singleton Arne Vajhøj <arne@vajhoej.dk> - 2017-12-23 19:27 -0500

csiph-web