Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton two or more instances

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, The following is asked me several times during the interview
Question : Can we have singleton with 2 instances or 3 instances or more ? How to achieve that ?
My Answer : It cannot be more than 1 instance.
But interviewers are never happy with my answer even though I gave them the Singleton code. Please let me know how to get it. Please post code which helps me a alot.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
 
fred rosenberger
lowercase baba
Bartender
Posts: 12198
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start a second JVM...
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I've always thought of the Singleton pattern as one that generally limits the amount of instances created, but not necessarily to a single instance. The name is misleading, but the general case makes more sense for a pattern.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Start a second JVM...

Or just use a second class loader?

It could be that they're thinking about distributed applications...in which case unless you're doing something clever there's nothing to stop the separate components each creating an instance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic