• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

enums are best for singleton- Need a convincing reason

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wherever I have read, it is mentioned that the best approach to ensure that singleton is achieved is by using enum.

But then what puzzles me is, enum can never be compared to a class, class has so many enriched features which an enum can never support. Examples:

1. Composition
2. Inheritance- it just allows to implement an interface

And there could be more.

Like for example SessionFactory in hibernate, you want to have a single instance of that per application, but then SessionFactory cannot be represented by just an enum.

And enum is not a full fledged class.



Can you guys tell me is that is correct? I mean enum cannot be used for the same in most of the scenarios, like for example I added SessionFactory in Hibernate.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Amar wrote:. . . class has so many enriched features which an enum can never support. Examples:

1. Composition
2. Inheritance- it just allows to implement an interface . . .

Where on earth did you get that from? Most people no longer use inheritance much. It is of course possible to use enums in composition.

And enum is not a full fledged class. . . .

Again, who told you that?

Horses for courses. An enum is designed for a particular purpose and a session factory for something different. You have convincinglly told me nothing about enums, but that you can't eat soup with a knife and fork. It is the wrong tool for that job.
 
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that all the use cases that you can't use an enum for, are really bad use cases in the first place.

You mentioned that you only want a single SessionFactory. Absolutely fine! Just create a single instance and pass it around. Keeping it in a static field and letting dependent classes perform service lookups makes your code very messy and buggy.

The singleton pattern is dangerous to use for entities and services. And for value types enum is often easier. Unless you want to use composition, but you can always ask: why do I want to limit the number of objects created in the first place?
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is of course possible to use enums in composition.


I think they mean you can't compose an enum of other objects, which is also not really true because you can give enums private constructors.
 
Saloon Keeper
Posts: 26286
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
I think they mean you can't compose an enum of other objects, which is also not really true because you can give enums private constructors.



 
Sheriff
Posts: 8523
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Stephan van Hulst wrote:
I think they mean you can't compose an enum of other objects, which is also not really true because you can give enums private constructors.





Just to clarify, enum constructors are private implicitly. What Stephan probably meant, that you explicitly can define one with parameters (private).

As in an example below.
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do this regularly with String parameters if I want to give an enum a different identifier than the one derived from its name. However, you can also compose it from other types. It's important that all types are immutable though, or that the enum only exposed unmodifiable views, because enums really should represent immutable value types.
 
Deepak Amar
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The reason is that all the use cases that you can't use an enum for, are really bad use cases in the first place.

You mentioned that you only want a single SessionFactory. Absolutely fine! Just create a single instance and pass it around. Keeping it in a static field and letting dependent classes perform service lookups makes your code very messy and buggy.

The singleton pattern is dangerous to use for entities and services. And for value types enum is often easier. Unless you want to use composition, but you can always ask: why do I want to limit the number of objects created in the first place?



Hello Stephan, if you see for example, Runtime class which is singleton but then they did not use enum for it? Its not easy to add all the functionality in enum, that's what I meant. But still enum is considered best.
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like I said, enums are for value types, and Runtime is not a value type. That does not mean that it was necessarily a good idea to make Runtime a singleton.

The purist in me says that Java main classes should have been required to extend some kind of Application class that encapsulates all the services for which only one instance exists during the life-time of the application. The main application class then decides for itself where to inject these services, instead of allowing random classes it doesn't know about to alter application state through globally accessible variables.
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Amar wrote:... if you see for example, Runtime class which is singleton but then they did not use enum for it?



Alsoi because Runtime has been in Java since the beginning and enum is a relative newcomer to Java.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic