Singleton Design Pattern Tutorial

Singleton Design Pattern TutorialWelcome 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

36 thoughts on “Singleton Design Pattern Tutorial”

  1. 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 (^-^)

    1. 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 🙂

  2. for making it more thread safe ,it would be better to declare firstThread as volatile in Singleton class :-
    static volatile boolean firstThread = true;

  3. 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…

  4. 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.

  5. 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?

    1. 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

  6. Just curios – Linkedlist inside the singleton, if made static and singleton class not be made singleton could have achieved the same ?

  7. Hi Derek,

    There are other ways of breaking Singleton Pattern
    1. Reflection
    2. using Cloneable

    Can you please explain these also?

  8. 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.

  9. private static final Singleton firstInstance = new Singleton();

    I think this gives same instance even for threads also.

  10. 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.

    1. sry, my bad, didnt look at the full code 🙁

      Comment gave me an impression that just that line meant tht.

      Thanks anyways

  11. 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

      1. 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

  12. 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

  13. 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

  14. what an awesome tutorial. you are a good guy.
    if you can please and an other java product tutorial(j2ee,j2me..
    Any ways thanks

  15. Fantastic job on design patterns. Never found any online tutorial so crisp, clean and effective so far. Good job and do please keep going.

Leave a Reply

Your email address will not be published. Required fields are marked *