A Singleton is an object that cannot be instantiated. There can be only one instance of a singleton created by the Java Virtual Machine (JVM).
Why is that useful, and why would you want to use such a thing…
Often in designing a system, we want to control how an object is used, and prevent others (ourselves included) from making copies of it or creating new instances. For example, a central configuration object that stores setup information should have one and one only instance.
Now that you know what a singleton class is, and some of its uses, I'll move on to the main topic of that tutorial – the singleton class pattern.
The Singleton Class Pattern
Preventing direct instantiation
To prevent direct instantiation, we create a private default constructor, so that other classes can't create a new instance.
Here is a default class definition, for a Singleton Object class.
Of course, you may add some more initialization code as needed.
public class SingletonObject
{
/*The Constructor*/
private SingletonObject()
{
/*No code required. may remain blank*/
}
}
Ok, that seems fine. However, there is no way to use that class currently.
We want to prevent direct instantiation, but we still need to allow a way to get a reference to an instance of the singleton object.
Getting an instance of the singleton
We need to provide an accessor method, that returns an instance of the SingletonObject class but doesn't allow more than one copy to be accessed. We can manually instantiate an object, but we need to keep a reference to the singleton so that subsequent calls to the accessor method can return the singleton (rather than creating a new one). To do this, provide a public static method called getSingletonObject(), and store a copy of the singleton in a private member variable.
public class SingletonObject
{
/*The SingletonObject Object*/
private static SingletonObject singletonRef;
/*The Constructor*/
private SingletonObject()
{
/*No code required. may remain blank*/
}
/*Method to get only one object of the SingletonObject*/
public static SingletonObject getSingletonObject()
{
if(singletonRef == null)
{
singletonRef = new SingletonObject();
}
return singletonRef;
}
}
We need to make sure that threads calling the getSingletonObject() method don't cause problems, so it's advisable to mark the method as synchronized. This prevents two threads from calling the getSingletonObject() method at the same time. If one thread entered the method just after the other, you could end up calling the SingletonObject constructor twice and returning different values. To change the method, just add the synchronized keyword as follows to the method declaration:
/*Method to get only one object of the SingletonObject*/
public static synchronized SingletonObject getSingletonObject()
{
if(singletonRef == null)
{
singletonRef = new SingletonObject();
}
return singletonRef;
}
Preventing the Singleton Object from being cloned.
Suppose one of the developers created a certain class, call it class A, and implemented the cloneable interface.
Public class A implements cloneable
{
//code….
}
Then, he decided to extend the SingletonObject class with class A.
Public class SingletonObject extends A
{
//code…
}
Now our SingletonObject also implements cloneable, and it is possible to clone objects, and thus ruining the idea of a Singleton object.
In order to prevent it, we must add our own clone method.
That way, if by chance someone extends the singleton object with a cloneable class, we will be able to handle it.
Here is the clone method to add to our class:
/*method to prevent cloning the SingletonObject*/
public Object clone() throws CloneNotSupportedException
{
/*preventing the singleton object from being cloned*/
throw new CloneNotSupportedException();
}
That is all for Singleton class. As you saw in that tutorial, creating a singleton class is quite easy.
Just block access to its constructor, prevent it from being cloned, and supply a method to get only one instance of that object.
Remember, This is only a general pattern. you may change it, and improve it to suit your needs
Full code for a Singleton Class
public class SingletonObject
{
/*The SingletonObject Object*/
private static SingletonObject singletonRef;
/*The Constructor*/
private SingletonObject()
{
/*No code required. may remain blank*/
}
/*Method to get only one object of the SingletonObject*/
public static synchronized SingletonObject getSingletonObject()
{
if(singletonRef == null)
{
singletonRef = new SingletonObject();
}
return singletonRef;
}
/*method to prevent cloning the SingletonObject*/
public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
}






MultiQuote





|