
Singleton Pattern
Author: Aiesha Adnan
This is my second journal, which i like to share with those who are looking for ways to use Singleton Pattern.
Motivation for learning (problem )
Design patterns are a new technique that is taught in this unit. Creational pattern deals with object instantiation. Factory pattern is the pattern I studied during the lectures. There are other patterns like Singleton Pattern that comes under the creational pattern. Currently I have no knowledge in this pattern. Besides, I have to apply design patterns in my assignments. I have no idea what it is. Therefore, I feel I should have knowledge in this pattern, if I am to use design patterns in the applications that I develop. Therefore I decided to learn about the single pattern, its structure, how to use it in java.
What is Singleton pattern? (insight Solution)
The singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process. Therefore it ensures that at any time there can be only one instance of a singleton(object).
Implementing Singleton pattern (plan of achieving the solutions encountered)
There is actually only one class that needs to be written specially to implement the Singleton pattern. A second client class will always be involved take make requests from the singleton.
Preventing direct instantiation
In order to prevent the direct instantiation create a class with the constructor marked as private. Therefore, those other classes cannot create a new instance.
public class SingletonObjectDemo{
private static SingletonObject singletonObject;
//Note that the constructor is private
private SingletonObjectDemo (){
// Optional Code
}
Using the singleton object
This is the client, which will use this pattern. It creates an object of the singleton object and use it.
public class SingletonObjectDemo{
public static void main(String args[]){
//create the Singleton Object..
SingletonClass obj = SingletonClass.getSingletonObject();
System.out.println("Singleton object obtained"); }
}
Overcoming some practical problems
1. Thread Problems
As with the current way of implementing the access method can be called twice from two different places simultaneously which will lead to the creation of more than one object at a time. It could happen that the access method may be called twice from 2 different classes at the same time and hence more than one singleton object being created. If this happens this would definitely violate the singleton pattern principles. Therefore, add the synchronized keyword to he method as below.
public static synchronized SingletonClass getSingletonObject()
2. Cloning
It is still possible to create a copy of copy of the Object by cloning it using the Object’s clone method.such as below.
SingletonObjectDemo clonedObject = (SingletonObjectDemo) obj.clone();
This again violates the Singleton Design Pattern's objective.Inorder to deal with this, it is necessary to override the override the Object’s clone method which throws a CloneNotSupportedException exception.This will ensure that cloning is not supported.
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
Final Implementation
class SingletonClass{
private static SingletonClass singletonObject;
/** A private Constructor prevents any other class from instantiating. */
private SingletonClass(){
// Optional Code
}
public static synchronized SingletonClass getSingletonObject()
{
if (singletonObject == null){
singletonObject = new SingletonClass();
}
return singletonObject;
}
public Object clone()throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
}
//this is the client program.
public class SingletonObjectDemo{
public static void main(String args[]){
// SingletonClass obj = new SingletonClass(); Compilation error not allowed
//create the Singleton Object..
SingletonClass obj = SingletonClass.getSingletonObject();
System.out.println("Singleton object");
}
}
Finally, there is an output, which indicates that the object is created.
Consequences of using it.
Singleton pattern can be used in any application which requires an object to be instantiated only once at a time.
Singleton pattern is best when developing a Java Help Module in a project can use a singleton pattern as at any time the developers can use with only one main Help object and use the same object in different screens.
Another usage would be to create a connection pool, which will help to avoid the wastage of resources where, a singleton pattern can be used to maintain a single connection object, which can be used throughout the java applications.
• State objects are often Singletons.
• Singletons are often preferred to global variables because:
o They don't pollute the global namespace (or, in languages with namespaces, their containing namespace) with unnecessary variables.
Conclusion
This journal has helped to understand the singleton pattern structure and how it can be implemented. Singleton pattern provides easy ways for unique object instantiation. In java, it is simple to create a singleton pattern. In the future projects I would be happy to apply singleton pattern.
Total Hours Spent in learning: 12 Hours 30 minutes