Singleton Design Pattern Tutorial

  Рет қаралды 461,097

Derek Banas

Derek Banas

11 жыл бұрын

Get the Code: goo.gl/0yVzM
Best Design Patterns Book : goo.gl/W0wyie
Welcome to my Singleton Design Pattern Tutorial. The Singleton pattern is both easy to understand as well as useful. I'll demonstrate first how to implement the Singleton pattern. Then I'll provide and example of its usefulness with a Scrabble example.
Threads can sometimes play havoc with this pattern, so I'll show you how to avoid those problems. I'll also review how to use threads, LinkedLists and more.

Пікірлер: 337
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) I'll definitely cover JAX-RS in an upcoming tutorial. Thank you for the request. I've been holding off on Java networking stuff, but I'll cover it completely soon
@derekbanas
@derekbanas 11 жыл бұрын
Your thanks is all I require. I have no interest in money anymore so don't donate money. If you think others would like the site tell them about it and I'll be happy :)
@derekbanas
@derekbanas 11 жыл бұрын
Thank you for the input :) I'd love to expand on the different pattern flavors available and will very soon when I cover refactoring. This was a fun tutorial to make and I hope it helps people
@derekbanas
@derekbanas 10 жыл бұрын
Thank you :) It is great to hear that I cleared everything up. That is the best compliment I could get.
@derekbanas
@derekbanas 10 жыл бұрын
Thank you :) I greatly appreciate the compliments. I'll try to live up to them. I'm getting very close to doing something very good. I can't wait to cover both games and the math behind them while at the same time cover electronics on a deep level. This is very fun for me.
@derekbanas
@derekbanas 10 жыл бұрын
Thank you very much :) I'm glad you found them useful. I will definitely cover enterprise design patterns, but I haven't covered JEE yet. I decided to make a truly complete java tutorial. i never figured it would take me 2 years to do it. At this rate I'll be making them for another 2 years since Android joined the tutorial.
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) I'm not done with design patterns. I'll be looking at them further in my OOD and refactoring tutorial. Thank you for the request and I plan on covering that topic
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) I used them as one source, but also a bunch of others as well as friends for this. I'm glad it helped
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) I'm not very good at scrabble, but I may make this into a fully functional game. I figured you guys would know how to do that though. Many game tutorials are coming!
@derekbanas
@derekbanas 11 жыл бұрын
Yes you are correct. I sometimes try to cover all of the bases in these tutorials. You got it though. Thanks for pointing that out :)
@derekbanas
@derekbanas 10 жыл бұрын
Thank you :) That is very kind to say. Yes I need to cover some of the topics that demand more attention Java wise. May God bless you as well.
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :) I'm using Eclipse right now so I can't test it, but yes I'm pretty sure if you just delete the code block with if(firstThread) you won't any problems. Give it a try. There won't be any hidden errors, you'll see what happens immediately
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) Yes, I'll cover Android as soon as I'm done with design patterns. Believe me the work you do here will make making android apps extremely easy
@shibaramsahoo
@shibaramsahoo 11 жыл бұрын
Thank you Derek for all your enthusiasm to put it together in a better way... We appreciate your hard work and your support. You Rock!! I just found your tutorials yesterday and been love with your website.. Great job!!
@derekbanas
@derekbanas 11 жыл бұрын
Yes I was just trying to make a fun little program with it
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) Believe me it makes me very happy to know I'm helping. Good luck on your test
@derekbanas
@derekbanas 10 жыл бұрын
Thank you for pointing that out. I definitely need to make a Java Concurrency tutorial. Ill do my best to keep improving :)
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :) I made a little typo there. Sorry about that, but thank you for pointing it out
@derekbanas
@derekbanas 11 жыл бұрын
You're welcome :) you use static classes when you are absolutely positive that every method will only be used in a utility type way. It is normally pretty easy to change a singleton if you require multiple instances down the road. That isn't true with a static class
@derekbanas
@derekbanas 11 жыл бұрын
I use both eager and lazy initialization. I can tell you know the difference from your comment. I didn't bring up the difference because this tutorial is meant to be an easy to digest design patterns tutorial. I haven't really talked about the Java memory model yet, but I'll definitely bring this topic and others when I do.
@derekbanas
@derekbanas 10 жыл бұрын
Thank you :) Yes I return firstInstance to make sure there is only one instance. Maybe I should have used a better name. Sorry about the confusion.
@xipity
@xipity 6 жыл бұрын
Subject's explanation is crystal clear. Minor mistakes that mentioned in comments aren't that distracting. Thanks!
@derekbanas
@derekbanas 6 жыл бұрын
I'm glad it helped
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) That is the first time I've received credit for saving lives :D
@derekbanas
@derekbanas 10 жыл бұрын
Thank you :) It is always nice to hear that I cleared a topic up. No, I don't teach anywhere but on youTube. I plan on staying here for a while and everything will always be free.
@derekbanas
@derekbanas 11 жыл бұрын
Thank you very much :) I try to do the best I can
@derekbanas
@derekbanas 11 жыл бұрын
Are you talking about the thread part? I ran both threads using the same code so there wasn't a need to write separate code for each thread.
@Laura147HLY
@Laura147HLY 9 жыл бұрын
Love your tutorials, they are quick and concise, and to the point. Excellent!
@derekbanas
@derekbanas 9 жыл бұрын
Laura147HLY Thank you very much :)
@Karthik-yy6up
@Karthik-yy6up 8 жыл бұрын
Never thought about the problems that could arise with threading. This is a beautiful tutorial. Thank you Derek.
@derekbanas
@derekbanas 8 жыл бұрын
+Karthik Iyengar Thank you :) I'm glad that it helped.
@javaidbolaky7817
@javaidbolaky7817 10 жыл бұрын
Thump up man, your design pattern codes example was really really useful. Its 100% understandable than a reading a lot of books without really see how the design should be. Many Thanks for your dedication.
@derekbanas
@derekbanas 10 жыл бұрын
Thank you for the input. I should have covered that here :)
@derekbanas
@derekbanas 10 жыл бұрын
I'm glad I was able to help :)
@nehaagrawal1730
@nehaagrawal1730 10 жыл бұрын
I came across your tutorials on youtube and I feel blessed that I have found you. Your tutorials are so helpful. God bless you!! Keep up the good work!! Please make some tutorials on Java Concurrency.
@antoniszewczyk5943
@antoniszewczyk5943 8 жыл бұрын
Hello Derek! Thanks for you tutorial again! What are the advantages and disadvantages of employing lazy instantiation? When should I use it and when it is not a good idea?
@arisapostoloy7267
@arisapostoloy7267 6 жыл бұрын
Your lessons are irreplaceable and exciting. Do not miss any elements of your personality. Keep calm and educate people.
@derekbanas
@derekbanas 6 жыл бұрын
Thank you for the nice compliment :) I'm happy it helped
@derekbanas
@derekbanas 11 жыл бұрын
Wow I'd like to see your program! I'm very happy if I was able to help :)
@derekbanas
@derekbanas 11 жыл бұрын
I'll have to look into that. Sorry about the error. They slip in every once in a while
@derekbanas
@derekbanas 11 жыл бұрын
It is used just as a precaution. Understand that some things that I do, I do because I was taught that way. We all pick up bad habits on our journey. I tend to always stay cautious :)
@QLTEQ
@QLTEQ 10 жыл бұрын
Oh man, these are great. So much that I've seen is making more sense now.
@derekbanas
@derekbanas 11 жыл бұрын
Sorry about that. I'm getting better at avoiding mistakes. Yes, thank you for the short cut. I know them, but I don't use them in the video because I thought they may confuse people
@derekbanas
@derekbanas 11 жыл бұрын
I hope my tutorials were able to help. I wish you the best of luck
@altapocalypse
@altapocalypse 10 жыл бұрын
Liked a part about the threads, good explanation of the problem! Thanks
@derekbanas
@derekbanas 10 жыл бұрын
Vadym Ko Thank you :)
@sambitkabi4617
@sambitkabi4617 7 жыл бұрын
And yes just one thing to add , you can also use the Bill Pugh's singleton implementation which avoids the double checked locking and the synchronization is handled by the JVM itself.
@MrSontiny
@MrSontiny 11 жыл бұрын
well, I really look forward to seeing the topic. Anyway, the tutorial is easy to understand for beginners like me. Thanks a lot. Gotta wait the tutorial that you mentioned above.
@derekbanas
@derekbanas 10 жыл бұрын
You're very welcome :)
@gopis1030
@gopis1030 Жыл бұрын
This playlist is really helpfull ... Thank you so much Derek ...can u please tell some examples for singleton pattern
@ankitajain4194
@ankitajain4194 9 жыл бұрын
Hi Derek nice explanation.Could you please also make one video of Singleton pattern using Enum and pros & cons of both approaches ?
@Truthiness231
@Truthiness231 11 жыл бұрын
Sitting here with my copy of Design Patterns (well trust and recommended), and have to say your coverage of usage is better. Thanks much ^.^
@derekbanas
@derekbanas 10 жыл бұрын
I will soon, but I have my hands full at the moment with Android and C
@sambitkabi4617
@sambitkabi4617 7 жыл бұрын
Great one..The intention of Singleton pattern is very well fulfilled.But one thing I would like to suggest is that in multi-threaded environment we need to make the instance variable volatile. Making it volatile will prevent the re-ordering of the instructions and thereby avoiding undesired behavior.
@heikomeyer2489
@heikomeyer2489 7 жыл бұрын
The whole series on design patterns from you is awesome - thank you very much for your efforts on that! However in my opinion I think they would be twice as awesome if you would simply slow down - say about half the speed. No need to change anything else since the content is already great. Just give us 3 or 4 seconds every time you would otherwise flip to the next screen. For now I'm constantly doing play/pause and my mouse may quit service soon ;-)
@derekbanas
@derekbanas 7 жыл бұрын
Thank you :) Sorry about the speed. I hope to revisit this topic soon.
@fedecacharelli
@fedecacharelli 10 жыл бұрын
in the video you typed the following line: System.out.println("2nd Instance ID: " + System.identityHashCode(newInstance)); so, you were calling the HashCode of the first instance.. (that was obviously going to return the same ID number) but in the source code you bring you've fixed this.. However, just wanted to thank you because you really helped me with these videos! Thanks again, greets from Argentina!
@rogerf.1758
@rogerf.1758 10 жыл бұрын
Hi Derek, really enjoy your tutorials. Helps an old C-programmer to get in to the modern design patterns. Though, just wanted to point out that it's not so good to use threads and this stuff directly. Where I work that is not allowed. We MUST use the concurrent packages to encapsulate all low level details. Perhaps that's a topic for some tutorials as well? Thaks for very understandable tutorials, I will see them all!
@FatalRescue
@FatalRescue 11 жыл бұрын
Whoa, I subscribed within the first 30 seconds....very cool presentation! You've saved lives :D
@IbrahimAmer92
@IbrahimAmer92 10 жыл бұрын
no problem at all :) ... but your tutorials are still the best on the internet .You really helped me a lot your tutorials are much more better than any design pattern book . Waiting from you more videos and tutorials ;)
@xiyuxie5215
@xiyuxie5215 8 жыл бұрын
Hi Derek, thank you so much for such an excellent explanation. Could explain in more detail why the second Synchronized method on Singleton.class is preferable?
@AlexTimification
@AlexTimification 11 жыл бұрын
Thank you so much, I'm making a ChessGame, this tutorial was so friggin useful for me. Thank you so much one more time^^
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :)
@sandhyaaa24
@sandhyaaa24 7 жыл бұрын
superb way of explaining....about all the program line by line!!! Really sometimes its hard to understand why something is done in certain way ad evething is use in some purpose!!
@derekbanas
@derekbanas 7 жыл бұрын
Thank you :) I'm glad it helped
@gujaalsmanda
@gujaalsmanda 8 жыл бұрын
great tutorial Derek i finally understood what each line does.
@derekbanas
@derekbanas 8 жыл бұрын
+gujaalsmanda Thank you :) I'm glad it helped.
@ajaisinghkhalsa
@ajaisinghkhalsa 9 жыл бұрын
Thanks Derek!!! Your tutorials are the best thing that happened to my life!! That was just some exaggeration, but they are pretty awesome.
@derekbanas
@derekbanas 9 жыл бұрын
Ajai Khalsa You're very welcome :) I'm very happy that I can help
@derekbanas
@derekbanas 11 жыл бұрын
Yes the code will only ever create one instance. Everything else is there to enforce that under any possible circumstance.
@michaelbrooks6713
@michaelbrooks6713 5 жыл бұрын
Excellent lesson Derek, Thanks!!!!
@derekbanas
@derekbanas 11 жыл бұрын
If everyone started off the day singing, just think how happy they'd be :)
@pkamleshkr
@pkamleshkr 8 жыл бұрын
Derek you are really awesome :) Your tutorials are veryyyy gooood !!!
@derekbanas
@derekbanas 8 жыл бұрын
+kamlesh kumar Thank you :) I try to do my best
@markovukolic8444
@markovukolic8444 10 жыл бұрын
Thank you Derek. You've helped me a lot. So, Thread.currentThread() is not necessary?
@sanjeevkumaremail
@sanjeevkumaremail 11 жыл бұрын
Hi..Videos are really helpful. Can you cover how singleton handles de-serialization ?
@dreamer6998
@dreamer6998 11 жыл бұрын
hello, Derek. once again, thanks for the great tutorials! one quick question about this tutorial: is it necessary in the Singleton class to have this if (firstThread){...}? you mentioned when you typed them in that they were needed so that the other threads had chances to mess with them. in the end of the tutorial, you synchronized the instance.class to stop other threads to mess around, but how about just deleting those if(firstThread){...} code? thank you very much
@derekbanas
@derekbanas 11 жыл бұрын
I mainly just write mods that blow stuff up and make characters and weapons. I use Minecraft Coder Pack and Mod Loader
@derekbanas
@derekbanas 11 жыл бұрын
It would be more like an Inactive Figure :)
@derekbanas
@derekbanas 10 жыл бұрын
I'm sorry. Some times I write out code to explain what is going on. I write much of this out of my head, so I tend to write code like I write sentences. Thread.sleep() says to put the current thread to sleep. So I was saying in code that I wanted the current thread and then I will put it to sleep. Sometimes by explaining to much I guess I can cause confusion. Sorry about that :)
@arthurmee
@arthurmee 10 жыл бұрын
Excellent video Derek. Thank you very much.
@derekbanas
@derekbanas 10 жыл бұрын
Thank you :) I'm glad it helped.
@mdfarooq7145
@mdfarooq7145 7 жыл бұрын
explanation is very neat and clean, Thank you Derek
@derekbanas
@derekbanas 7 жыл бұрын
Thank you :)
@derekbanas
@derekbanas 11 жыл бұрын
It may take a bit to get it. This is pretty high level programming. It definitely isn't a basic tutorial
@ananth314
@ananth314 11 жыл бұрын
Very good and informative video. Derek- I have a request. Can you please make some videos on Web Services? This is the area I couldnt follow even after reading so many books and articles.
@XCasperChristensenX
@XCasperChristensenX 8 жыл бұрын
You are my hero! best supplement for my school book(head first design patterns) - i must say u describe the things better! keep it comming! :D
@derekbanas
@derekbanas 8 жыл бұрын
+Casper Christensen Thank you for the nice compliment :) I'm glad they are helpful.
@stevenh3574
@stevenh3574 8 жыл бұрын
Thanks Derek, you break things down really well. I'm glad I found this video. Researching OO Design Patterns just got a bit easier. Are there any OO Design Patterns that are common with mobile app development?
@derekbanas
@derekbanas 8 жыл бұрын
+Steven Harden Thank you :) I pretty much cover patterns you'll use in mobile development. I need to cover enterprise stuff, but that is on the server.
@shogun301
@shogun301 9 жыл бұрын
Derek! Great videos (all of them)...Do you by chance have anything about the Volley Library in Android..??
@nicoapinedo
@nicoapinedo 4 жыл бұрын
I have to say, first of all, your tutorials are great, in this particular one there was only one section that had some issues and wasn't reflecting what you were stating during that part of the video (even though your statement was right, just not for that code). Around 11:00 the lines 21-27 for ScrabbleTest.java should be modified to: System.out.println("Instance 2 ID: " + System.identityHashCode(instanceTwo)); System.out.println(instanceTwo.getLetterList()); LikedList playerTwoTiles = instanceTwo.getTiles(7); System.out.println("Player 2: " + playerTwoTiles);
@vijaykonduri1370
@vijaykonduri1370 6 жыл бұрын
To complete the circle you may want to stop cloning of ur object and also the serialization and deserialization as well.
@opkarthik
@opkarthik 10 жыл бұрын
Nicely done
@vaironxxrd
@vaironxxrd 11 жыл бұрын
They asked a lot about design patterns and oop concepts along with logic in my interview
@chongliu9339
@chongliu9339 9 жыл бұрын
thank you Derek , your video is great, helps me a lot!
@derekbanas
@derekbanas 9 жыл бұрын
chong liu You're very welcome :)
@discursion
@discursion 10 жыл бұрын
There is a mistake in your code when you are demonstrating that both "newInstance" and "instanceTwo" are sharing the same IdentityHashCode. In fact, you are printing twice the same object ID. I know it normally gives the same result, but you should add a note.
@demetrioreylago6856
@demetrioreylago6856 10 жыл бұрын
Also the first player tiles are printed twice.... it looks like both players obtain the same set of letters which is not true.
@RolopIsHere
@RolopIsHere 10 жыл бұрын
I noticed the same!
@liedebunker1253
@liedebunker1253 6 жыл бұрын
You are right. He made a mistake, although he is very good.
@omer8836
@omer8836 6 жыл бұрын
Derek Banas buddy you tutorials are helpful but I guess in this tutorial you made a mistake, you should definitely look into it, you are printing out the same object identityHashCode(newInstance) and playerOneTiles
@mikesaddi
@mikesaddi 11 жыл бұрын
Great video! BTW, I noticed that you wanted to get 7 letters, but used "
@derekbanas
@derekbanas 11 жыл бұрын
If you find it useful in your design use it. If not don't. What you'll find over time is that there are many people that like to argue about this stuff because they think it makes them look smart :)
@stepho670
@stepho670 7 жыл бұрын
Can you be my professor? The world needs more people like you!
@derekbanas
@derekbanas 7 жыл бұрын
Thank you for the compliment :) I'm here for free when ever you need me
@detectivedonaldkimball
@detectivedonaldkimball 4 жыл бұрын
Derek I have some Qs: In Singleton class, in the method: "public LinkedList getLetterList(){ return firstInstance.letterList;}" Can you just return letterList instead of firstInstance.letterList? It seemed to work the same when I tried. Also, in this example couldn't we just make ScrabbleLetters a static field, and thus avoid having to create a singleton class? Finally, instead of having a nullcheck in getInstance method, can't we just instantiate the firstInstance field where it is declared? Would that also make it thread safe rather than using synchronized?
@15bigdave
@15bigdave 8 жыл бұрын
I'm on mobile so I'm not sure if anyone has said anything. in your get tiles loop, it should be strictly less than instead of less than or equal to number of tiles. right now you're returning 8 tiles for getTiles(7).
@derekbanas
@derekbanas 8 жыл бұрын
+15bigdave Thank you for pointing that out. I'll look at it
@Lesterffx
@Lesterffx 8 жыл бұрын
Thank you Derek I have learnt some useful methods which I never used before, such as Arrays.asList() and System.identityHashCode(). Also, I learn how to use the keyword/reserved word, synchronized.
@derekbanas
@derekbanas 8 жыл бұрын
+Lester You're very welcome :) I'm glad it helped.
@StefioaneCy
@StefioaneCy 10 жыл бұрын
Watched a video in a course about singleton. It was 10 minutes long. I watched it five times and couldn't understand how singleton pattern works. I watched your video and got it in 3 minutes. I don't know if you do, but you should be teaching!
@TheSynergism
@TheSynergism 11 жыл бұрын
Thank you... It was clearly explained..
@rptechplex
@rptechplex 9 жыл бұрын
Nice explanation. Just an observation at 10:37, hashcode of instanceTwo had to be printed, for clarity sake, isnt it? :)
@derekbanas
@derekbanas 9 жыл бұрын
Raghavendra Prakash Yes I'm using it to show that the object is the same
@redsnakeintown
@redsnakeintown 8 жыл бұрын
Well you should have printed the hashcode of instanceTwo..you tried to print both places the same "newInstance"
@kbrendarizo
@kbrendarizo 7 жыл бұрын
yes, that´s right, instancetwo isn´t used
@abedrazem2260
@abedrazem2260 9 жыл бұрын
Thanks Derek!Very Nice tutorials! just want to ask can inherit Singleton class
@derekbanas
@derekbanas 9 жыл бұрын
Abed Razem Your welcome :) you could, but once you have an instance of a singleton you won't be able to override the derived type
@rajrocks89
@rajrocks89 6 жыл бұрын
Hello @Derek Banas, I absolutely love all of your videos! Thanks for creating such great content. Is it possible for you to share the slides? Or, publish all the slides in an ebook format?
@derekbanas
@derekbanas 6 жыл бұрын
Thank you very much :) Sorry, but I don't have the slides. I would gladly give them away for free
@rajrocks89
@rajrocks89 6 жыл бұрын
No problem!! I guess making notes instead will help me :P
@markovukolic8444
@markovukolic8444 10 жыл бұрын
Derek, inside getLetersList method at 6:25, you are returning leterList object like this "return firstInstance.letterList" instead "retun letterList". Why? Is it beacuse you want to be sure that only one object have access? Thank you, you are fantastic.
@obuseng87
@obuseng87 11 жыл бұрын
Thanks derek,u hv been really helpful.r u ever goin to cover android dev?
@AnmolSingh-yf8ed
@AnmolSingh-yf8ed 7 жыл бұрын
Awesome videos mate! Thanks a bunch! :)
@derekbanas
@derekbanas 7 жыл бұрын
Thank you very much :)
@586naveen
@586naveen 7 жыл бұрын
best explained...... got the concept of Singleton... thanks
@derekbanas
@derekbanas 7 жыл бұрын
I'm happy it helped :)
@smuralimohan1
@smuralimohan1 11 жыл бұрын
Thanks Derek for the energetic videos. I have a question related to the concept of Singleton. In a singleton, If we were to maintain only a single 'instance' of a class, why maintain an instance at all? Why can't we just make the constructor private and make the rest of all methods static? What is the advantage of creating a single instance versus using a (static) class with all static methods?
@utsavprabhakar5072
@utsavprabhakar5072 3 жыл бұрын
Love the question man. I'll think about this and get back to you. I think this has something to do with concurrency and simultanous use of class variables. I'll have to check. Thanks again :)))
Builder Design Pattern
13:05
Derek Banas
Рет қаралды 325 М.
Iterator Design Pattern
22:47
Derek Banas
Рет қаралды 172 М.
Increíble final 😱
00:37
Juan De Dios Pantoja 2
Рет қаралды 107 МЛН
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 33 МЛН
Children deceived dad #comedy
00:19
yuzvikii_family
Рет қаралды 6 МЛН
Visitor: How I Mastered the Toughest Programming Pattern
12:59
Command Design Pattern
23:41
Derek Banas
Рет қаралды 282 М.
Singleton Design Pattern | Implementation with details & code ✌🏻
21:09
Observer Design Pattern
22:27
Derek Banas
Рет қаралды 659 М.
Template Method Design Pattern
14:05
Derek Banas
Рет қаралды 147 М.
How to BREAK and FIX Singleton Design Pattern | Interview Question
19:32
Daily Code Buffer
Рет қаралды 29 М.
Visitor Design Pattern
13:31
Derek Banas
Рет қаралды 276 М.
Singleton Design Pattern in C# - Do it THAT way
13:15
tutorialsEU - C#
Рет қаралды 22 М.
State Design Pattern
20:51
Derek Banas
Рет қаралды 238 М.
Composite Pattern - Design Patterns (ep 14)
1:11:23
Christopher Okhravi
Рет қаралды 142 М.