• 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

what are singleton classes ?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain it
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sunanda kadam:
Please explain it



Singleton is a Design pattern for allowing only one instance of your class.
(i.e.,) You are allowed to create only one Object for your class.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton is a design pattern to create a single(or some numbers) instance of class by keeping constructor of class as private.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
according to set theory when a set contain only one element is called a singletone set. example A={5}

so single tone class is a class which can able to create only single instance of that class.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not to forget about a static factory method, often named getInstance(), which acecesses the private constructor on demand.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The better version of singleton template is:

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And just slightly better:

Mind you, singleton objects usually should not be Serializable, and most definitely not Cloneable.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you might just as well make the singleton instance public static final, which will save you the getInstance() method alltogether ...

One good reason for initializing it on demand is resource issues: Don't create the object unless you need it. At least for large singletons.
[ May 03, 2008: Message edited by: Guido Sautter ]
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then it's ok to not have the field be final, but then you shouldn't initialize it upon declaring. If you initialize a singleton when you declare it, it's most logical to make it final.

But in your code it is not wise to make it final
It does suffer from one possible problem: if multiple threads access the method at the same time, it is possible to create two instances. You'll need some form of synchronization, the simplest being to just synchronize the entire method.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote

--------------------------------------------------------------------------
And just slightly better:

code:
--------------------------------------------------------------------------------

private static final SingletonExample instance = new SingletonExample();


--------------------------------------------------------------------------------

---------------------------------------------------------------------------

above code can be considered when you have multithreade enviornment.
you need to add some synchronization check in your getinstance method ,which will prevent race condition.
lazy creation of object gives you better performance when you do not require the object frequently.There it gives performance benefit.


Examples of singleton , Runtime class in java is singleton,Printer,there are other several examples of singleton.

________________________
Mintoo
SCJP 1.4
____________________________
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never encountered a case where lazy instantiation made sense, so I would always instantiate the class as in Rob's example. This is thread safe and requires no synchronization of any methods.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/SingletonPattern
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic