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.
If you like videos like this, it helps to tell Google [googleplusone]
Sharing is nice [nttfblike]
If you need to brush up on Java Threads, or Java LinkedLists I have you covered.
Code from the Video
Singleton.java
ScrabbleTest.java
ScrabbleTestThreads.java
GetTheTiles.java
Nice work Derek,,
I think that the singleton design pattern can add more elegance to the observer design pattern if used with the subject class, so that only one instance of the subject class created and used by many observers.
Just an opinion (^-^)
Absolutely you can definitely write better code by combining the patterns. I’ll get into combining patterns later along with spotting when you should use them. Many more videos on this topic are coming. Thanks for the input 🙂
like allways. the best.
Thank you 🙂
for making it more thread safe ,it would be better to declare firstThread as volatile in Singleton class :-
static volatile boolean firstThread = true;
Thank you for the input 🙂
How can i download complete project? Here I didn’t find any link where i can download total project at once… Please let me know… thank you…
I’m not sure what you mean. The code I used follows the video
Awesome, The best tutorial I have ever came across .You have covered materials that has never been touched by anyone else.your background in the field is SOLID.A+
Thank you for the time and efforts.
Thank you very much 🙂 I like covering topics that for some reason everyone ignores
Derek, What is the difference between this Design Pattern series of video and the OOP design series?
What is the c++ equivalence of java interface?
The design patterns tutorial just covers common design patterns. The Object Oriented Design tutorial teaches how to think about designing an object oriented program that is easy to understand and manage.
To create an interface using C++, look into virtual methods. I hope that helps
Very helpful; tutorials.. Thanks.
You’re very welcome 🙂 Thank you for stopping by
Just curios – Linkedlist inside the singleton, if made static and singleton class not be made singleton could have achieved the same ?
Hi Derek,
There are other ways of breaking Singleton Pattern
1. Reflection
2. using Cloneable
Can you please explain these also?
Hi,
I cover cloneable in my prototype design pattern tutorial. I cover reflection in my java reflection tutorial. I hope they help 🙂
Very nice tutorial …I feel this is the best of all what I could found on internet 🙂
Thank you very much 🙂 I’m glad you enjoyed it.
This is really good with nice explanation.
But one suggestion , instead of taking that much lengthy code in getInstance() method of Singleton.java to test with threads we can just do with the below code.
public static Singleton getInstance() {
if (firstInstance == null) {
try {
Thread.currentThread();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
firstInstance = new Singleton();
return firstInstance;
}
This returns two different instances.
Thank you for the input 🙂
private static final Singleton firstInstance = new Singleton();
I think this gives same instance even for threads also.
Hi , Thanks for tutorials.
I think there is a problem in following statement:
“// Here we just use synchronized when the first object
// is created
synchronized(Singleton.class){ ”
The snippet synchronized(X.class) uses the class instance as a monitor. As there is only one class instance (the object representing the class metadata at runtime) it is used to make sure that there is exactly one Thread in the block.
sry, my bad, didnt look at the full code 🙁
Comment gave me an impression that just that line meant tht.
Thanks anyways
Derek, you did a great job with Singleton. I’ve heard others say that you can also use enum and doing so can make for a more simple Singleton effect. What are your thoughts on enum for the purpose of Singleton?
Many thanks,
Dan
I think it is a perfectly good solution especially if you know what you’ll need ahead of time.
Hi Derek,
This is really helpful. I am also watching your android tutorials- they are great too.
Can you please do a tutorial with trying Singleton with an enum and in general more tutorials on how to break/ test the design patterns.
Thanks!
HG
Thank you 🙂 I’m going to do my best to fit in more design pattern, algorithm, data structure videos very soon.
Hi Derek Thank you very much for these delicious videos,
I learned a lot of necessary and cool stuff that all programmers or engineer must know.
You re the best teacher i ‘ ve ever seen on Internet.
Continue working i will be working with you.
Cya
Thank you for all the nice compliments 🙂 I’m glad you enjoy them.
Hi Derek,
Good video and examples. I have one feedback. If you can put a copy button for copying code without line letters. It would be great. It is time consuming to delete line numbers. Or you can direcly give link to java source.
Thanks
Thank you 🙂 If you put your mouse over the upper right corner of the code and click View Source you can eliminate the line numbers. Hope that helps
what an awesome tutorial. you are a good guy.
if you can please and an other java product tutorial(j2ee,j2me..
Any ways thanks
Thank you J2ee is coming soon
Fantastic job on design patterns. Never found any online tutorial so crisp, clean and effective so far. Good job and do please keep going.
Thank you very much 🙂 I’m happy that you found the videos useful.