Singleton Design pattern is all about making sure that only object can be created per class loader hierarchy.
There are some situations where we want only one object in our application such as cache object, Hibernate SessionFactory object which contains configuration.In these Conditions creating multiple objects in applications are wast of memmory.
Here is example of How we can make sure that only one object can remain in application to avoid memmory wast.
.Date Util
public class DateUtil {
private DateUtil() {
// no-op
System.out.println("constructor...");
}
This is simple class .Anyone can create n number of Objects from above class using new Operator.
We want to restrict our application can contain only one object of DateUtil class.
Step 1.
The basic Step of creating object everyone knows DateUtil dt=new DateUtil();nwe must stop user to creating object using new operator.
answer in make constructor private and let the user get object through factory method.
public class DateUtil extends AppClass implements Cloneable {
private static DateUtil instance;
private DateUtil() {
// no-op
System.out.println("constructor...");
}
public static DateUtil getInstance() {
if (instance == null) {
instance = new DateUtil();
}
}
return instance;
}
}
Here we restricted user to create n number of objects using factory method.Everytime when user need object he will call DateUtil.getInstance() method to get object.
It Seems that this is perfect example but still there are some ares where we need to workout.
This programm can provide single object in non multi threaded environment but in mutithreaded environment problm may arise..
Lets Assume tha 2 Threads are trying to create object at same time they check condition]
if (instance == null) {
they will found object is nout created thy may create new object.
There are some situations where we want only one object in our application such as cache object, Hibernate SessionFactory object which contains configuration.In these Conditions creating multiple objects in applications are wast of memmory.
Here is example of How we can make sure that only one object can remain in application to avoid memmory wast.
.Date Util
public class DateUtil {
private DateUtil() {
// no-op
System.out.println("constructor...");
}
}
This is simple class .Anyone can create n number of Objects from above class using new Operator.
We want to restrict our application can contain only one object of DateUtil class.
Step 1.
The basic Step of creating object everyone knows DateUtil dt=new DateUtil();nwe must stop user to creating object using new operator.
answer in make constructor private and let the user get object through factory method.
public class DateUtil extends AppClass implements Cloneable {
private static DateUtil instance;
private DateUtil() {
// no-op
System.out.println("constructor...");
}
public static DateUtil getInstance() {
if (instance == null) {
instance = new DateUtil();
}
}
return instance;
}
}
Here we restricted user to create n number of objects using factory method.Everytime when user need object he will call DateUtil.getInstance() method to get object.
It Seems that this is perfect example but still there are some ares where we need to workout.
This programm can provide single object in non multi threaded environment but in mutithreaded environment problm may arise..
Lets Assume tha 2 Threads are trying to create object at same time they check condition]
if (instance == null) {
they will found object is nout created thy may create new object.