Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #37908
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Guice vs plain singleton |
| Date | 2017-12-23 19:27 -0500 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <p1msag$m6p$1@gioia.aioe.org> (permalink) |
| References | (2 earlier) <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> |
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
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