Singleton pattern is a popular design pattern which proposes that only one instance of a class will be created by JVM.Repeated calls always return the same instance.When programmers need to be concerned about meaningful use of resource then use this pattern.For example ,if programmers create a new connection object every time it requires , then clearly it is a wastage of resource.In this case we can create a singleton connection class and that single connection object will be used throughout the application.
If you need connection object just use it like this....
Another example where singleton is used frequently is in hibernate when you instanciate SessionFactory.Because SessionFactory is very resource intensive.So instanciate it only once.
I hope this will help you.For more info go to this link Singleton Pattern
Please give me a brief explanation of how the database program is using one instance to save memory. Also, please put the code in tags for ease of reading.
I cannot use the hibernate example because i have not yet learned that.
There might be cases where you will have utility methods which do not depend on the availability of the instance but are methods related to the class- like obtaining the count of the instances created so far.
Mohamed Sanaulla wrote:There might be cases where you will have utility methods which do not depend on the availability of the instance but are methods related to the class- like obtaining the count of the instances created so far.
Rahul Sudip Bose wrote:
I am not clear about utility methods.
Suppose you have a class- say a class mapped to a certain table in the database- we would call it as a model class. And you want to add a method to retrieve all the rows which satisfy a certain criteria. As this doesnt require an instance to be created, we mark this method as static.
Utility methods are methods that have nothing to see with the actual business logic of the application.
They only exist to 'help' the programmer. Because they don't use class fields, they can be left static.
and the output of that code is : Same reference......
The reason is when ever the ConnectionUtil class is loaded into the memory that static block is initialized and connection object is created.How many objects you create it doesn't matter , same reference will be given to every object .
Rahul Sudip Bose wrote: I am not clear about utility methods.
Imagine you have a class that have a method and it always run in the same way , it's sole purpose is to return some thing ,doesn't matter which instance of that class perform that method.These type of methods are called utility methods.Utility methods are static methods.For example ,Math class has several utility methods like random(),exp(double),log(double)
etc.We don't need create an instance of Math class( you can't create an instance of Math class because it has private constructor).Use these methods by using class name only.
Post by:autobot
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, that will fit in a stocking