Youtube Channel

Singleton Class

Singleton class
Definition: A class for which only one object is created and used in entire application is called singleton class.
Why we need singleton class?
As in java we have to fixed the functionality of a class allover the application we go for singleton class.since creating multiple object is waste of memory
Eg: As we know in jdbc we make properties files which have the properties or functionality related to database connectivity.To connect to database we require an intermediate class which reads the data from the property file. This class is the example of singleton class. As the single connection object to used or created to be used in entire application.
How to make a singleton class?
There are several approaches to make a class a singleton
Approach 1:
Make the class as static with its properties and methods,along with it make a private constructor.
Benefits of making a static class .–à As the class will be static it will get instantiated during the class loading to jvm and will get memory in Heap area i.e. Permgenerator(Permanent generator of heap).
So no need of object creation as its properties (variables) and functions can be called using the class name.
Benefits of making a private constructor-à As we will introduce a private constructor the object creation for the class outside will not be possible.
So, the class finally becomes the singleton class.
E.g:
Public class Demo
{
Private Demo();
Private static int id;
Private static int name;
Private static void m1()
{——-
}
}//end of class

Approach 2
If we want to make a class as a singleton then object cration to that class should not be allowed form outside the class. To restrict an object creation we need to make the constructor of that class as private.
  • We need to create a object of that class inside the class and then we need to define a public static factory method to return the one object to the other class.
Public class Demo
{
Private static Demo demo=new Demo();
Private Demo(){}
Public static Demo getInstace()
{
Return demo;
}
}//end of demo class

Public class simple
{
Public static void main(String args[]){
Demo demo1=new Demo();
Demo demo2=new Demo();
System.out.println(demo1==demo2);
System.out.println(demo1.hashCode()==demo2.hashCode());
}
}//end of simple class

Output: True    True
Note:- here both objects are sharing same functionality i.e functionality is fixed for Demo class
Approach 3:
  • In the above program when one object of class created at class load time. It means the object is early created.
  • The drawback to early creation is if the object is not used by another class then object memory is wasted

In this approach we create the object of the singleton class inside the static factory method so, that we can create the one object lazily and hence we can avoid the memory wastage problem of the above approach.

Eg. In multithereading environment if two threads are simultaneously calling the static factory method then two object of singleton class will be created so the singleton principle is violated.
  • to solve the concurrent issue, we need to make the static factory method as a synchronized method.
  • if a method is synchronized then only one thread is allowed to access it simultaneously so that the object for the class will be created only once .
Public class Demo
{
Private static Demo demo=null;
Private Demo(){}
Public Synchronized static Demo getInstace()
{
If(demo==null)
{
Demo=new Demo();
}
Return demo;
}
}//end of demo class
Approach 4:
  • The above singleton class is an appropriate way of making a singleton class but if another class clones an object of the singleton class than one more object of singleton class will be created. it means again singleton principle is violated.
  • To make a java class as 100% singleton class, we should not allow object cloning.
  • in order to restrict the cloning we should override the clone method in singleton class by implementing that class form cloneable interface.
  • E.g
Public class Demo
{
Private static Demo demo=null;
Private Demo(){}
Public Synchronized static Demo getInstace()
{
If(demo==null)
{
Demo=new Demo();
}
Return demo;
}
public Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
}//end of demo class
Next PostNewer Post Previous PostOlder Post Home

0 comments: