What is Singleton class
Singleton is a design technique which gaurantees that there will be only instance of a class globally. Such classes are required when we need to create some objects which are memory/ resource extensive and we can't afford many such objects.
Using singleton We can maintain single object per JVM per classloader. Classloader could be different in different hierarchy and in such case we may have more than one instances of singleton but we can avoid it by loading it at appropriate classloader. For example in an ear application there could be multiple web modules and each one of them will have their own singleton instance. Sometimes it may be a need but sometimes it could be a flaw which can be resolved by loading it either at ear level or web module level as per requirement.
Implementing singleton class
There are two different ways to implement singleton in java.Using singleton design pattern
In this pattern we can create singleton either using lazy loading or using eager loading.
Using enum class
Singleton creation is very easy using enums. Just create an enum class as given below. You can also create parameterized Enum instances by adding the relevant constructor.
In lazy loading, we create the instance when we actually need it. But implementation is little tricky and we need to handle thread safety. Below is the example of lazy loading implementation.
public class MySingleton{ private static MySingleton instance = null; private MySingleton(){} public static MySingleton getInstance(){ if(instance==null){ synchronised(MySingleton.class){ if(instance==null){ instance=MySingleton(); } } } return instance; } }
In eager loading, we create the instance as soon as class is loaded. Implementation is easy here and we don't need to worry about thread safety. But it creates the object whether we need it or notBelow is the example of lazy loading implementation.
public class MySingleton{ private static MySingleton instance = MySingleton(); private MySingleton(){} public static MySingleton getInstance(){ return instance; } }
public enum MySingleton { instance; }And then you can get the instance by just using below code.
MySingleton i = MySingleton.instance;
Best approach to create singleton
You already saw here that you can create singleton either of the above approaches but there are some differences between them.With singleton design pattern class creation need to handle synchronization. Also there is a chance that it can be serialized/ deserialized or class variables accessor can be modified using reflection which will destroy the singlton.
In my opinion using Enum for singleton will be the better option as it will automatically handle the above problems.
Thanks for sharing a really needed post that i am looking for , if anyone looking for best java institute in delhi so join with us visit our website : https://www.htsindia.com/java-training-courses or contact us : +91-9311002620
ReplyDeleteThanks for sharing this informative post. It's really kind of content that I'm looking for a very helpful post by the way. If anyone looking for best Sas training institute in Delhi Contact Here-+91-9311002620 Or Visit our website https://www.htsindia.com/Courses/business-analytics/sas-training-institute-in-delhi
ReplyDelete
ReplyDeleteThis is really a good source of information, I will often follow it to know more information and expand my knowledge, I think everyone should know it, thanksccna practice test
Thank you for sharing an amazing & wonderful blog. This content is very useful, informative and valuable in order to enhance knowledge. Keep sharing this type of content with us & keep updating us with new blogs. Apart from this, if anyone who wants to join the SAS Training institute in Delhi, can contact 9311002620 or visit our website-
ReplyDeleteBest SAS Training Institute in Delhi | Best SAS Training Institute in Noida (htsindia.com)
If you're looking to excel in the field of networking, CCNA Training in Noida at APTRON is your gateway to success. In this SEO-friendly article, we'll delve into why APTRON's CCNA training in Noida is the ideal choice for aspiring network professionals.
ReplyDeleteThe curriculum of the Software Testing Training Course in Noida is regularly updated to incorporate the latest industry trends and technologies. Students can expect to learn about manual testing, automation testing, performance testing, and much more. The course also includes comprehensive training in widely used testing tools such as Selenium, JIRA, and TestNG. This equips graduates with the skills required to stand out in a competitive job market.
ReplyDelete