Singleton design pattern in Java - Part 2

  Рет қаралды 62,811

Ranjith ramachandran

Ranjith ramachandran

Күн бұрын

In this video I have explained how to fix flaws we saw in part1.
Along with fixes there are discussions such as: Double checked locking, Whats wrong with double checked locking and how it is fixed,
enum as Singleton and difference between singleton class and class with static methods.
Please comment if you have any questions or points to discuss. Please subscribe if you like the content.
#java #singleton #designpatternsinjava #javaprogramming #javainstitute #enum #macbookair #easy #tips #fyp #fypシ #fypシ゚viral #trending #tutorial #artificialintelligence #interview #india

Пікірлер: 161
@ahmetyasarozer
@ahmetyasarozer Жыл бұрын
Having met many experts in the field, and watched thousands of videos, believe me, Ranjith and his vidoes are my favorite. I feel like the world kind of hides a diamond inside it. Ranjith, I'm sending my love and appreciation to you. Wish you a beautiful life, together with your family and kids, who are, I'm sure, brilliant as much as you.
@MrJonnis13
@MrJonnis13 8 жыл бұрын
This man is undoubtedly a Java Guru. Deserves much more views and subscribers !!
@marekbugiel6893
@marekbugiel6893 7 жыл бұрын
Excellent job. Very well explained. Taking into account that this is a free video makes your work even more impressive. Respect.
@DurgaShiva7574
@DurgaShiva7574 3 жыл бұрын
Sir you are GOD of java tech.... and that too free content of this level, hats off !!
@jating4you
@jating4you 3 жыл бұрын
your every word is pure and contains tons of experience/information, thanks for creating such videos. please create more.
@samjam_93
@samjam_93 4 жыл бұрын
Mass Thala, Never seen such an explanantion, Just by seeing the few videos you posted gained immense knowledge. Love from madurai :)
@akshaygupta2863
@akshaygupta2863 3 жыл бұрын
Great explanation i have never seen such a good explanaiton. Great job. Can u please some videos on Thread to improve thread concepts
@shashankshekharkaushik1567
@shashankshekharkaushik1567 7 жыл бұрын
This Person deserves a thumps up !
@ansoribrahim6833
@ansoribrahim6833 4 жыл бұрын
very well and extensive explanation. some tutorial hard to explain basic concepts, but this video can explain complex concepts clearly. respect
@darshanpatel9663
@darshanpatel9663 4 жыл бұрын
I have never seen this much deep explanation for Singleton..Great.Thank you so much.Waiting for other design pattern videos.
@pranavdixit8577
@pranavdixit8577 6 жыл бұрын
Really Great Work, Sir !! I am in this field for around 5 years and I can undoubtedly say that this is BEST tutorial for anyone to learn Singleton in all the depths possible !! Cheers !!
@talktosmruti
@talktosmruti 4 жыл бұрын
@7:29 "But still I would prefer throwing exception thing. Because ....Personal choice"....That's called swag with intelligence....
@itskumaresan
@itskumaresan 6 жыл бұрын
Appreciate your valuable thoughtfulness in explaining the issues with Singleton and fixing the same!!! Best one that is available out there!!! Keep up the good work!!!
@shaleenmadlapure1405
@shaleenmadlapure1405 3 жыл бұрын
very well explained
@mritunjayyadav3788
@mritunjayyadav3788 4 жыл бұрын
your explanations and knowledge in the topic is off the charts sir !
@AdityaSingh-kn2hf
@AdityaSingh-kn2hf 5 жыл бұрын
Great explanation and handled all the possible scenarios for Singleton. Thumbs up!!!!
@siddharthdg
@siddharthdg 8 жыл бұрын
Good and precise content , please keep the videos coming .
@vaibhavshukla9297
@vaibhavshukla9297 9 жыл бұрын
Really .. really helpful video to understand the singleton and its complex behavior in different scenarios in one go... thanks a lot !!!!
@aravindraj8893
@aravindraj8893 5 жыл бұрын
Detailed Explanation with easy Example. Thank you, Sir
@akashagarwal6390
@akashagarwal6390 3 жыл бұрын
Underrated channel, trust me when i have been trying to dig deepest into Java since a couple of yrs. This is yrs of knowledge in this video. Do you still make videos? Pls do...
@amitosh14mishra
@amitosh14mishra 8 жыл бұрын
Really Nice one , Gone through all your video... w8 for more.
@JustmeAgain89
@JustmeAgain89 7 жыл бұрын
I was literally clapping at the send of the video , excellent intro to Singleton Pattern , thanks a lot . Can you if possible , throw more insight into the best practices for multi-threaded coding in Java or general.
@vasuudayar3691
@vasuudayar3691 3 жыл бұрын
never seen this much deep explanation. i wonder why u stop making videos. u helped us alot Thanks
@RiyazParasara
@RiyazParasara 5 жыл бұрын
Awesome
@hcabra
@hcabra 9 жыл бұрын
Thanks Ranjith for this excellent material on Singleton. I believe this is one of the most common yet misunderstood pattern in programming. Your video series simplifies many of the complex notions built around it. Kudos.
@ranjithramachandran5928
@ranjithramachandran5928 9 жыл бұрын
happy2k thanks for the feedback; yes, most of us takes singleton as the example of design pattern they used, meanwhile it is very hard to get right!
@gunturmanju
@gunturmanju 6 жыл бұрын
Excellent Video Ranjith. Best one so far on web which explains in detail with code from violation standpoint. Thank You!
@utkarshgupta2274
@utkarshgupta2274 2 жыл бұрын
This was really an in-depth video about the Singleton pattern. I would say this was the best explanation I have seen till now. Great work Ranjith. I request you to please upload more videos on your channel. So that we can learn and grow from your knowledge.
@rajumohan8827
@rajumohan8827 3 жыл бұрын
Hello Sir...your session is really motivation for us. Explanation is really well. Nowhere I can find this much of clear and crisp Explanation. Could you please continue making videos for us latest technologies.
@omtodkar
@omtodkar 8 жыл бұрын
FAN of your teaching !! crystal clear with Singleton concept ! great work on video. Keep it up dude !!!
@janardhannallapati
@janardhannallapati 8 жыл бұрын
@Ranjith i found your videos have the best explanation on Singletons i found so far. Thanks , keep up the good work.
@manendranath
@manendranath 7 жыл бұрын
One of the best explanations I have ever seen....Thanks Ranjith
@creative-freedom
@creative-freedom 8 жыл бұрын
One of the best java videos on youtube. Hats off !
@pahariya09
@pahariya09 4 жыл бұрын
Awesome sirji.. you have put lot of efforts to explained.... and very nicely explained..
@guessmewhoiam
@guessmewhoiam 9 жыл бұрын
Thanks Ranjith for crystal clear about Singleton.Most of the doubts got cleared!!!!
@saktiranjanbehera7010
@saktiranjanbehera7010 5 жыл бұрын
Thanks for the facts and solution given after first video
@rohitsharma-xt8qe
@rohitsharma-xt8qe 5 жыл бұрын
Very well explained with crystal clear concepts. Thank you so much for this sir. :)
@scodz2007
@scodz2007 8 жыл бұрын
It was really awesome, really enjoyed both the episodes. Keep up the good work.
@quadcom1
@quadcom1 7 жыл бұрын
Ranjith you are awesome. Thanks for your practical explanation.
@shreyatiwari1331
@shreyatiwari1331 4 жыл бұрын
Ranjith, you explained things very well why we have very less number of videos from your end. Awaiting more such videos to learn Java from your end..
@prakashkchauhannn
@prakashkchauhannn 7 жыл бұрын
Nice and Simple explanation. Thank you.
@praveenpemal6503
@praveenpemal6503 7 жыл бұрын
thanks sir , the singleton videos are best waiting for more sir....
@suresh85nit
@suresh85nit 8 жыл бұрын
Really Really Awesome Explanation..... I have ever heard.. FYi I have subscribed.. Thank You So much for sharing knowledge..
@prajaktajawarkar6037
@prajaktajawarkar6037 5 жыл бұрын
Thanks Ranjith for such an awesome explanation. This is very helpful for every one who want to learn Singleton pattern. Even all your videos on java are excellent. But those are very limited. I am requesting you to please upload java videos as many you can so that it will help us a lot to improve our knowledge...Please..Thanks again for such a wonderful videos.
@SivaKumar-cx7db
@SivaKumar-cx7db 8 жыл бұрын
Excellent, no words to say.
@rohitborate9824
@rohitborate9824 4 жыл бұрын
Answer for situation where constructor called prior to getInstance method using Reflection: -> One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.
@nithesh53
@nithesh53 7 жыл бұрын
Nice one
@xen_ofc
@xen_ofc 6 жыл бұрын
Very cool video.
@dsingh4800
@dsingh4800 7 жыл бұрын
great work ! perfect that you explained that subject so thoroughly with many different aspects, thank you :)
@sanjaybharkatiya
@sanjaybharkatiya 8 жыл бұрын
hi ranjith..very very nice and detailed explaination..i have watched all of your vedios...please publish vedios of such kind of expalination for all design patterns you have sumerized in high level distribution of design patterns in one of your vedios...awating of your next vedios on designp
@peterbittu865
@peterbittu865 6 жыл бұрын
Super Ranjith , videos are good and informative.Please make videos on other design patterns as well apart from the factory you have made.
@vishalradhanpara6866
@vishalradhanpara6866 4 жыл бұрын
I think we are still able to break. After adding 'throw RuntimeException' in constructor, if we use multi-threading for Reflection we can create 2 distinct objects static void useSingleton() { try { Class clazz = Class.forName("com.patterns.Singleton"); Constructor ctor = clazz.getDeclaredConstructor(); ctor.setAccessible(true); Singleton s3 = ctor.newInstance(); print("s3", s3); } catch (Exception e) { } } Invoking above method with 2 threads, we see 2 different objects created :)
@Sk-zs1cw
@Sk-zs1cw 6 жыл бұрын
Thank you so much for sharing video.
@HariKrishna-bv5hs
@HariKrishna-bv5hs 8 жыл бұрын
It's simply Awesome explanation.. Thank you Ranjith.
@hemalathadinesh1830
@hemalathadinesh1830 6 жыл бұрын
Excellent Job, Hats off
@24035709
@24035709 7 жыл бұрын
How can you be so awesome? Hands down, this is one of the best tech channel I subscribed in recent times. Are you on facebook or twitter ? I don't want to miss any updates from you
@rajeshkundum5622
@rajeshkundum5622 6 жыл бұрын
Man, you rocked. Great explanation. keep going.
@meghaladevi6411
@meghaladevi6411 5 жыл бұрын
Thank you so much Ranjith. Hats off to you.
@snehaldhabale1893
@snehaldhabale1893 5 жыл бұрын
Hello Sir, your all videos are so well explained and very helpful. Could you please post more videos.
@shubhamchaurasia367
@shubhamchaurasia367 4 жыл бұрын
Very well Explained Sir.. But why u stopped making videos sir..
@ketankankapure8530
@ketankankapure8530 3 жыл бұрын
Sir, could you pls continue making videos on rest of the design patterns as well ?
@DhrumilShahDhrumil
@DhrumilShahDhrumil 8 жыл бұрын
Thanks a lot.. its helps me a lot to understand the concept.
@gurmeetchawla8362
@gurmeetchawla8362 8 жыл бұрын
ranjith excellent explaination about singleton. I have seen other videos from you also. All of them are very well covered. Thank you for doing this. I have a request for you. kindly cover units tests also in some video. Please cover various possible situations in that like how to test static classes and static methods. Also how will you go about testing the web application controllers and database related code classes
@vipinjainoo7
@vipinjainoo7 8 жыл бұрын
Thanks for the videos. Its very helpful.
@anugantigowtham
@anugantigowtham 8 жыл бұрын
great tutorial with clear explanation. why new videos are not coming ?
@krish0007
@krish0007 6 жыл бұрын
Very good explanation..!
@IndrajeetKumar-ns8we
@IndrajeetKumar-ns8we 6 жыл бұрын
please make more videos, it's very less. your videos have depth analysis of concept which is helpful for working professionals also
@naveenkumar-tb1de
@naveenkumar-tb1de 5 жыл бұрын
Excellent ,this really helped.
@SunilManickalal
@SunilManickalal 8 жыл бұрын
Thanks for the videos. very helpful.
@naughtyrishan
@naughtyrishan 8 жыл бұрын
This solution is fine only. We can also modify the constructor part and that will suffice. Like: private Singleton() { if(soleInstance != null) { throw new RuntimeException("instance already exists. Thus this re-creation is not allowed."); } else { soleInstance = new Singleton(); System.out.println("creating singleton instance"); }
@rajeshnarasimha9926
@rajeshnarasimha9926 7 жыл бұрын
Thanks a lot, very well explained..
@raghuveerk166
@raghuveerk166 6 жыл бұрын
Hello Ranjith, I am missing your videos. I guess all the subscribed persons are also missing your videos. I tried searching your profile in other social websites as well. Can you please come back to youtube? Its also fine if you have paid video series we are willing to enroll, let us know any series if you have. Thanks once again. Awesome work.
@sreekanth9050
@sreekanth9050 8 жыл бұрын
Excellent Tutorial Ranjith. Really Appreciate your in depth Knowledge on this concept. Please Suggest the books that you refer to get this kind of in depth analysis on Core Java Concepts. Thank you and once again super knowledge sharing keep continue..
@MrVenkat521
@MrVenkat521 7 жыл бұрын
Hi Ranjith, Its great explanation, appreciate that. Any thoughts on if new thread try to load it with Class.forName("package.Singleton.class") at some places of invocation in the application will that creates new object/instace for new threads? and what happens to existing instances/reference?
@ckm5101
@ckm5101 7 жыл бұрын
Its Very useful , if possible can you please explain multi threading in next vedeo
@sudharshannallamothu3873
@sudharshannallamothu3873 8 жыл бұрын
We are awaiting for you next release of other design patterns Mr.Ranjith :)
@GiridharKN
@GiridharKN 9 жыл бұрын
best tutorial :) awesome thanks alot Ranjith :)
@user-bu8sr7fm3t
@user-bu8sr7fm3t 2 жыл бұрын
To be honest, I think this is the very definition of overthinking
@harshamr3712
@harshamr3712 6 жыл бұрын
Hi Ranjith,. Thanks for the clear information on Singleton Design pattern , Here i have tried the way you mentioned when we move the reflection code before creating the first two instances. and executed then Its creating an instance for reflection as well which has a different hash code and proceeding with creation of next 2 instances with different hash code for both instances . Please advise ,. on how can we overcome this.
@NaveenKumar-tt6en
@NaveenKumar-tt6en 5 жыл бұрын
Thank you so much for the awesome tutorial :)
@anshulgoyal7687
@anshulgoyal7687 8 жыл бұрын
Hi Ranjit, Awesome Explanation on Singleton Design Pattern....Here, it is not clear the flexibility benefit.Can u explore these also.
@yakoobshaik3980
@yakoobshaik3980 7 жыл бұрын
Thank you Ranjith for such a good videos. As you said, we'll have a discussion in comment section regarding the solution for violating singleton rules by creating the instance(s3) and then s1, s2 using Reflection. How can we restrict multiple instances and maintain Singleton pattern? Please answer.
@sriramkumar6673
@sriramkumar6673 4 жыл бұрын
did you find the solution ?
@rohitborate9824
@rohitborate9824 4 жыл бұрын
One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.
@TheDibyendusarkar
@TheDibyendusarkar 8 жыл бұрын
Another question that is coming in my mind, Object.clone() function can be called if a class implements "Cloneable" interface. Thus instead of restricting a singleton class to be cloned by overriding the "clone" function can't we simply avoid implementing "Clonable" interafce? Thanks in advance
@murugesanrethiam3951
@murugesanrethiam3951 6 жыл бұрын
nice....
@utkarshjadhav7
@utkarshjadhav7 7 жыл бұрын
Thank you very much for the tutorial Ranjith. Very informative. Apart from the tutorial, I was curious what font do you use in your IDE. Could you please tell the name of the font?
@PradeepMishra-nj5fm
@PradeepMishra-nj5fm 8 жыл бұрын
In which conditions we need a Single ton class to be serializable or clonable . If we do't want than can we prevent it by not implementing the interfaces ...
@javedakhtar-om2hb
@javedakhtar-om2hb 8 жыл бұрын
Great Explanation. qq#. When we use Reflection to get an Instance, If we keep the reflection code before those Singleton.getInstance(); We are getting the different Instances for Reflection and also for Singleton.getInstance(). How can we restrict to create more than one Instance using Reflection also.
@anirbanchatterjee4794
@anirbanchatterjee4794 5 жыл бұрын
use like:- private Singleton() { throw new RuntimeException("Instance already created please use getInstance()"); } private Singleton(boolean actualCreationCall) { System.out.println("Only Object Created"); } public static Singleton getInstance() { if(onlyObject == null) { onlyObject = new Singleton(true); } return onlyObject; }
@arnab567
@arnab567 6 жыл бұрын
awesome!
@nishitaramanuj3892
@nishitaramanuj3892 7 жыл бұрын
Hi Ranjith, thums up for your efforts, which tool did you use to record this video? Thanks, Nishita
@kranthipinninti9086
@kranthipinninti9086 6 жыл бұрын
Hi Brother :) Your videos are very good. Can you make other design pattern videos
@piyushasopa9932
@piyushasopa9932 9 жыл бұрын
Awesome Explanation on Singleton Design Pattern.......... Thanks Ranjith, I will be very happy if you come up with few more design pattern tutorials, Are you thinking of this?
@ranjithramachandran5928
@ranjithramachandran5928 9 жыл бұрын
+Piyush Asopa Thanks. Please subscribe to the channel for updates. I already have one on factory -method, please check it out kzfaq.info/get/bejne/e8p5qql8lc2nnWg.html
@VinodKumar-ng3kc
@VinodKumar-ng3kc 5 жыл бұрын
Best tutorial!!!
@avinashvutukuri652
@avinashvutukuri652 9 жыл бұрын
Really great work Ranjith, learning so much from you tutorials. I have a question, not sure if this is a possible scenario but I wanted to get it clarified. In the example where you implemented synchronization by double checking the condition to make it thread safe, let us say there are 2 threads both passed the first condition and the 1st thread entered the synchronized block and the other is waiting. Now the 1st thread passed the 2nd if condition as well and at this time just before creating the instance let us say the thread is preempted by the OS and 2nd thread is scheduled to run. The 2nd thread passes through the condition as well because instance is not yet created , and goes ahead and creates the instance. Once it exits the first thread might resume and create one more instance which violates our aim. So, is it possible for a thread to be preempted by OS in a synchronized block?? if it can be , will that thread relinquish the lock so that other threads can enter the same critical section?? Thanks in advance !!
@ranjithramachandran5928
@ranjithramachandran5928 9 жыл бұрын
+Avinash Vutukuri - the given scenario can be a possibility. However, 2nd thread still can not enter synchronized section as thread1 still has the monitor(lock) with it. So 2nd thread will be blocked until 1st thread completes. Only object.wait() will cause the lock to be relinquished (the locked acquired on object) AFAIK
@TheDibyendusarkar
@TheDibyendusarkar 8 жыл бұрын
Thanks Ranjith for the awesome video. Solution for the multiple class loader problem has not been addressed. Can you put some light on same.
@wBacz
@wBacz 7 жыл бұрын
more patterns, please.
@PrasadPofali
@PrasadPofali 5 жыл бұрын
I have a doubt. What do you mean by "safety against Serialization and Deserialization"?
@uniquestreetfood8391
@uniquestreetfood8391 7 жыл бұрын
Hi Ranjith, I have a question. If we have a scenario where we are having 2 threads, one thread using reflection to get an instance and other using global point of access getInstance() method. Can it still break the code u have provided to take care of reflection.
@anupanandi8186
@anupanandi8186 7 жыл бұрын
Firstly thanks for tutorial Ranjith.What if the reflection call is there before getInstance method() call? There two instances of Singleton class is getting created.How to prevent that?
@rohitborate9824
@rohitborate9824 4 жыл бұрын
One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.
@naveenaryal1400
@naveenaryal1400 2 жыл бұрын
@@rohitborate9824 @Rohit Borate Okay but how to get hold of the instance already created? In case the instance is created through reflection is there any way to get hold of that instance in Singleton class?
@pk062
@pk062 7 жыл бұрын
In double check locking method, if thread 1 has already created an instance, doesn't that mean it has acquired a lock on Singleton class while doing so? If so, other threads wont be able create a new instance anyways, so why the double null check? I know it's there in theory, but practically i feel only a null check and a lock on class level should suffice
@marekbugiel6893
@marekbugiel6893 7 жыл бұрын
Thats explained around 13:15.
@abhilashk9824
@abhilashk9824 6 жыл бұрын
It's the best explanation i have ever seen on the web about the singleton design pattern. I have one query, the way you have followed to avoid singleton breaking by reflection will not work if the first instance is created by reflection and second instance is created by getInstance() method. Could you suggest any way to avoid this as well?
@sharathchandra2940
@sharathchandra2940 5 жыл бұрын
In the constructor, if you are not throwing an exception, you can assign the value of the private static variable as this, like below private static Singleton singleton; private Singleton() { if (singleton != null) { throw new RuntimeException("Cannot create an instance of Singleton, please use getSingleton method"); } singleton = this; }
@rohitborate9824
@rohitborate9824 4 жыл бұрын
One way is by creating a Static boolean flag and set it TRUE in constructor when constructor executes successfully for the first time. You can use this flag in getInstance method to check if object is already created or not. If flag is TRUE that means object is already created and you don't need to create an Object. Here you can throw runtime Exception.
@VAR2024
@VAR2024 5 жыл бұрын
You are genius
@wrongL
@wrongL 6 жыл бұрын
Guru Dev ki JAY!!!!!
Singleton Design Pattern in Java - part I
26:25
Ranjith ramachandran
Рет қаралды 137 М.
Equals and hashcode contract
13:50
Ranjith ramachandran
Рет қаралды 86 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 41 МЛН
Это реально работает?!
00:33
БРУНО
Рет қаралды 4,2 МЛН
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 10 МЛН
Harley Quinn's plan for revenge!!!#Harley Quinn #joker
00:49
Harley Quinn with the Joker
Рет қаралды 21 МЛН
Singleton Pattern - Design Patterns (ep 6)
20:09
Christopher Okhravi
Рет қаралды 248 М.
About java.lang.String, why String is special in java - String tutorial
39:19
Ranjith ramachandran
Рет қаралды 109 М.
HashTable vs HashMap vs Concurrent HashMap all kinds of Map implementations
41:26
Design Patterns Master Class | All Design Patterns Covered
3:46:08
Daily Code Buffer
Рет қаралды 56 М.
Rabbi's incredible answer STUNS audience (with Dennis Prager)
15:19
Rabbi Manis Friedman
Рет қаралды 15 М.
Bulkhead Pattern - Fault Tolerant Microservices
8:43
Defog Tech
Рет қаралды 51 М.
JVM ( java virtual machine) architecture - tutorial
29:16
Ranjith ramachandran
Рет қаралды 514 М.
ThreadLocal in Java
10:59
Defog Tech
Рет қаралды 183 М.
Java Records: What are they?
18:54
Ranjith ramachandran
Рет қаралды 2,4 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 41 МЛН