• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Is this the right way to implement Singleton design pattern?

 
Ranch Foreman
Posts: 2348
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton design pattern is to be used when we want to ensure that only 1 instance of the class should be created. For e.g if we have only 1 liscence for something. Is the below the right way to implement it ?




thanks.
 
Sheriff
Posts: 16042
266
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, although the field is usually called instance not singleton
 
Monica Shiralkar
Ranch Foreman
Posts: 2348
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I have seen the below kind of code too for singleton but did not understand whether it will have any benefit over the one I originally posted.

 
Marshal
Posts: 26300
80
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or this way:


 
Marshal
Posts: 71730
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . whether it will have any benefit over the one I originally posted. . . .

No, it hasn't. It isn't thead‑safe. I think the only good way to create a singleton is what Paul showed you.
There is doubt about whether a singleton is a pattern or an anti‑pattern.
 
Junilu Lacar
Sheriff
Posts: 16042
266
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There is doubt about whether a singleton is a pattern or an anti‑pattern.


It can be a pattern or anti-pattern; it just depends on how you apply it. That said, many applications of Singleton are anti-patterns because they cause more problems than they solve.
 
Monica Shiralkar
Ranch Foreman
Posts: 2348
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Or this way:




And how would rest of the code be?
 
Campbell Ritchie
Marshal
Posts: 71730
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:.. .And how would rest of the code be?

Like this:-
 
Junilu Lacar
Sheriff
Posts: 16042
266
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:And how would rest of the code be?


That is the bare minimum you need to implement an enum singleton in Java. Obviously, if your singleton has some behavior that it needs to exhibit, the rest of the class would be whatever is needed to implement that behavior.
 
Monica Shiralkar
Ranch Foreman
Posts: 2348
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. So this means that code which I had originally pasted is required only for pre java 5 compiler and from java 5 onwards the code is a very short one using Enum as shown.
 
Paul Clapham
Marshal
Posts: 26300
80
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
Yes.
 
Monica Shiralkar
Ranch Foreman
Posts: 2348
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Paul Clapham
Marshal
Posts: 26300
80
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a good use case for the mport static statement. You can either write



or you can write



Which suggests that you might want to name the singleton instance something which describes its purpose more accurately than just "INSTANCE".
 
Campbell Ritchie
Marshal
Posts: 71730
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . pre java 5 compiler

Have you got a pre‑Java5 compiler? Remember Java5 is over sixteen years old.

. . . using Enum as shown.

Not Enum, which is a class, but enum please.
 
reply
    Bookmark Topic Watch Topic
  • New Topic