Design Patterns 2 : Singleton
So welcome with the second post on Design patterns and this is gonna be about the Singleton Pattern. In the last post you might remember how I have mentioned the birth of the design patterns and how Developers used it for their convenience and standardization of codes. This will be the first design pattern that will be discussed.
The word Singleton means a Single person. From that itself you can take the Idea about the Singleton.
If you remember the previous post (The table with the dimensions Scope and Purpose) you'll see that Singleton belongs to the Creational Design Pattern and to the scope of Objects. Let's discuss this further.
The Singleton Design Pattern
Singleton is one of the simplest design Patterns out there. The Singleton Design Pattern is used when once instance of an object is used throughout the entire life span of the program. A singleton design associates with a single class and only one single object is initiated from that class, in the program we make sure that we are having a way to access that object without creating a new object.
It'll we easy if we look at the implementation rather than trying to explain it in here.
Implementation of Singleton Design Pattern
With few simple steps we can implement a simple program with Singleton Design pattern
- Create an private constructor so that no one else can create object explicitly.
- By using Static Methods we can provide a global way to access the instance so there is no any necessity to create objects.
Let's get to the Coding.
First I'll Create a class with an private constructor disabling explicit creation of objects.
public class MySingletonClass {
//Create the only available instance of the MySingletonClass
//make it private and static
private static MySingletonClass SingletonInsance = new MySingletonClass();
// making the constructor private
private MySingletonClass(){}
//Since the object is private we need a method to return the object
//method needs to be public and static
public static MySingletonClass ReturnInstance(){
return SingletonInsance;
// returns the only available object
}
//A methods to test the Singleton Class
public static void write() {
System.out.println("This is Anjula Testing the Singleton");
}
}
To test this one we have to create another class.
For testing purpose let's create a class and try to create a MySingletonClass Object
For testing purpose let's create a class and try to create a MySingletonClass Object
You can see there that It'll be issuing an error saying that constructor is not visible. That is because we made it so by turning it to a private constrictor
. The output will be This is Anjula Testing the Singleton
Instances:
public class SingletonTesterClass {
public static void main(String[] args) {
//class to get the singleton object
// trying to create an object explicitly
/*MySingletonClass msc = new MySingletonClass();*/
// get the only available instance
MySingletonClass msc;
msc = MySingletonClass.ReturnInstance();
//test for the write method
msc.write();
}
}
. The output will be This is Anjula Testing the Singleton
Actual Implementation of Singleton Pattern
A few months ago I went for an Internship Interview and the First question regarding the Design Patters was this. Name some actual implementations of Singleton Design Pattern.Instances:
- Clip Boards
- Windows Registry
- Sharing one device with multiple users.
- FIle Systems.
That is it with the Singleton Design Pattern. Lets discuss something else in next post...