Implementing Singleton in java is easy! Isn't it?
Today i came across things which you can screw
up using a singleton pattern. Now I find it very
complex than being so easy. There are some special
cases where our "so called Singleton" is not actually
a singleton.
If one uses lazy initialization of singleton instance,
then you are very likely to create more than instances
of the singleton class. in this case, the method giving
you the instance of the singleton needs to be thread safe,
or we might end up in a situation where two threads try
to get singleton and parallely create to different objects.
But It has performance overheads in some cases.
Other problem is of having singleton objects in the
distributed environment. Objects may span across
JVMs Which creates lot of problems since the
Singleton objects are unique for a JVM. In case
where we use cluster of application servers we might
face this problem.
There are lots of other factors affecting singletons
in java for detailed text read this
on
Thursday, September 14, 2006
Labels:
code
,
Design patterns
,
Java
0
comments
Singleton pattern in JAVA?