• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can you please provide a real-time singleton class using Enum example in java?

 
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks in advance
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Believe it or not, I think you will find the Java® Language Specification helpful there.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "real-time"? I suspect it's not what you are really asking for.
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The official Java Trail on Enums may be of some help here:
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

However it does not go into using a Singleton class from what I can tell. To get the most out of this you may want to start here at this point in the Java trail:
https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do singletons have to do with enums?
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:What do singletons have to do with enums?



Using an enum is one way of creating a singleton.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arun Singh Raaj,

1. Better is to ask question in a post rather than subject line. For a reason so you could provide more details.
2. As you see from the replies you got so far, your question wasn't clear or not clear enough to provide clear answer/-s.

Please use post area to provide full details about what you mean, so the guys wouldn't need to make guesses on what you mean.
An example would be on a helpful side.
 
Arun Singh Raaj
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologise that my question wasn't clear.
As we know that Singleton pattern is used for Loggers, caching etc. I need an example of singleton pattern (using Enum) to implement a Logger class in a project.  
thanks for your help.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:singleton pattern (using Enum) to implement a Logger class


When you say implement Logger class, you mean your own logging framework?

Consider using existing logging frameworks as Apache Log4j 2. I'd be doubted you could easily come up with something better than that.

Arun Singh Raaj wrote:I need an example


Here
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have heard some people say that the singleton “patt‍ern” doesn't simply mean there is on instance; it means there are a fixed number of instances, each distinct from all others. Even Wikipedia mentions this:-

The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.

If you enhance the concept of singletons to include classes with fixed numbers of distinct instances, isn't that what an enum is? Every enum ever seen matches that description.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Singleton pattern has gotten itself a bad rep, as it has been way overused. Maybe it's sufficient to just instantiate the object you have in mind once?

I'm not sure which parts of a logging framework you think are (or should be) singletons - log4j, for example, uses static members in each class. Would those not serve your purposes?

Also, I second Liutauras' suggestion to use an existing logging framework instead of rolling your own.
 
Arun Singh Raaj
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help.
 
Arun Singh Raaj
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir, I have one more question regarding Singleton Pattern.
Assume, I developed an e-commerce application, if I make a particular class(Shopping Cart-class) Singleton in project, then will this class return one object per customer or a single object in the entire application for all customers?
I mean, Singleton refers single object per client or only object for all the clients?
 
Marshal
Posts: 28177
95
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
That depends on how you implement your so-called "singleton". But in real life it would be better to implement your class in the way your design says it should work; don't worry about attaching the description "singleton" to the class. If you need one object per customer, write it that way. Or if you need one object for the entire application, just put it into the servlet's application scope. What you're doing here is exactly what leads to the "bad rap" which Tim referred to.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic