Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton Pattern

 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone help me explain "How a Singleton pattern implemented in JVM"?
Thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't fully understand the question. Could you please elaborate? What do you need this for?
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been recently asked about singleton pattern which i aptly explained. Following that I was asked "how is singleton pattern implemented in JVM?"
I know that we can implement the singleton pattern for class A by checking if an instance is already present and then create it.
But i myself did not get that question..so posted it if anyone of you could help me out...
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are numerous variants. You can use lazy instantiation, so the Singleton is created only the first time you call getInstance(). In a web container, class reloading may mess up a Singleton based on static variables and you may be better off using the application context instead. This example allows only one instance of a class; sometimes you want one instance per possible state, the GoF book uses the example of using objects to represent the 26 letters of the alphabet; your Singleton will then have 26 instances, one for each letter.
- Peter
[ December 30, 2002: Message edited by: Peter den Haan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic