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.
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command
ReplyDeleterpa Training in tambaram
blueprism Training in tambaram
automation anywhere training in tambaram
iot Training in tambaram
rpa training in sholinganallur
blue prism training in sholinganallur
automation anywhere training in sholinganallur
iot training in sholinganallur
Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read.share some more info.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one,thanks lot!!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training