Let’s consider a scenario; many people have me to teach Java. It would be challenging to spend time and effort to train them individually. So, I asked them to watch my Java course on YouTube. Here, Singleton pattern has been used.
Here, people have asked me to create many objects (Java coaching for each member separately) for the same context, but I have said that Java coaching is the same for everyone. So, please go and watch my Java course on YouTube (Singleton object).
Singleton allows just one-time object creation so only one time I am going to post the videos. That video will be watched for N number of times when the bits of help is needed. So here, the object doesn’t have the specific properties. It has a universal property so it can be used any number of times. This is the real-time use case of Singleton.
Student Lenin=new Student (1,100);
Here,1 is the role number, and 100 is the marks.
Student Chenchu=new Student (2, 100);
Here, Chenchu is the object, 2 is the roll number, and 100 is the marks.
(1,100) and (2,100) are specific properties of Lenin and Chechu respectively.
So we shouldn’t use Singleton for this use case.
If the object doesn’t have its own specific properties, then the Single Design Pattern is applicable.
It controls multiple object creations by allowing only one-time creation.
public class Singleton { private static Singleton singleton; /** * Create private constructor */ private Singleton() { } /** * Create a static method to get instance. */ public static Singleton getInstance() { if (singleton == null) { singleton = new Singleton(); } return singleton; } public void displayMessage() { System.out.println("I have called using singleton object"); } }
We need to define Singleton object as private and static. Private here means the private access modifier which is used to make the object inaccessible by the outside classes. For example, if you need to access the private file, you’ll need the support of the methods.
You need to create the constructor as private so that the N numbers of constructors that are created are also inaccessible by the outside classes.
You need to create a static method to get the instance. Using this method you could get the object of this class. This is the important part. It will check whether the object of the static class is statically created or not.
For the first time, the Singleton object would be null. So the safe check would be paused and it will allow the execution of this line. The Singleton object would be created using the private class constructor. Though private constructor can’t be accessed outside the class, it can be accessed from within the class because we can access the private methods within the class.
So, using this principle we are accessing the private constructor and activating the object. If the method is called for the second time, when it will do the null check, the Singleton wouldn’t be null. It will consist of some object.
The last step is, we need to get the object using getInstance() method which can be named as anything like getInstance(), getObject(), getNew() etc.
So for the first time, it will check whether it is null or not. Since it will be null, it will use the private constructor to create the object and return the top object.
For the second time, since the object was already created, it will skip these steps and directly return the Singleton object which was created before.
So the object had to be created for the first time only and then it was used N number of times repeatedly, even if we have N number of reference variables.
By using these objects, we will call the display method. If we run the program, you’ll see the following output which is called using Singleton object.
public class Singleton { private static Singleton singleton; /** * Create private constructor */ private Singleton() { } /** * Create a static method to get instance. */ public static synchronized Singleton getInstance() { if (singleton == null) { singleton = new Singleton(); } return singleton; } public void displayMessage() { System.out.println("I have called using singleton object"); } }
public class Singleton { private static Singleton singleton= new Singleton(); /** * Create private constructor */ private Singleton() { } /** * Create a static method to get instance. */ public static Singleton getInstance() { return singleton; } public void displayMessage() { System.out.println("I have called using singleton object"); } }