Skip navigation.
Home

singletone

can any one tell what is singleton pattern and at what scenario we use it.
i only know that in singleton pattern we can create only one object for a class but for what purpose?

Pesonally, I like using a

Pesonally, I like using a calss full of static members than a singlton pattern.

---- Amr Kourany Software Engineer

It is more than static methods

The Singleton Pattern (as you have already pointed out) is nothing more that
ensuring a class has only one instance, and provides a galobal point of access
to it. That's it !
If you need to have a certain type of data shared to all objects in your
application and this data turned out to be unique, then you will need to use
this pattern. (e.g. connection to certain resource (database, regisrty, drivers
, ... ).

How to implement this pattern in Java is another thing. If it is so simple
and you have small primitive member variables, then relying on static initializers
and having all your methods static would be a solution yet it is not
recommended.

It gets somehow complicated when it comes to threading and multiple class
loaders.

This article it quite to the point. You would need only to read the first
page if multithreading is not a concern to you.

http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html