• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Arun Singh Raaj
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks in advance
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Believe it or not, I think you will find the Java® Language Specification helpful there.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66258
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "real-time"? I suspect it's not what you are really asking for.
 
Pete Letkeman
Ranch Foreman
Posts: 829
23
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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
 
Tim Moores
Saloon Keeper
Posts: 3953
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do singletons have to do with enums?
 
Dave Tolls
Ranch Foreman
Posts: 2996
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Liutauras Vilda
Marshal
Posts: 4789
330
BSD
  • Mark post as helpful
  • send pies
  • 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: 31
1
  • Mark post as helpful
  • send pies
  • 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: 4789
330
BSD
  • Mark post as helpful
  • send pies
  • 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: 56197
171
  • Mark post as helpful
  • send pies
  • 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: 3953
94
  • Mark post as helpful
  • send pies
  • 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: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help.
 
Arun Singh Raaj
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22691
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!