The singleton

While I agree that a Singleton shouldn’t be used to often, I do want to share my favourite way to implement the singleton pattern for reference.

public class Example
{
    private static Example instance;

    private Example()
    {
        // this class cannot get instantiated
    }

    public static Example getInstance()
    {
        if (instance == null)
        {
            instance = new Example();
        }

        return instance;
    }
}

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.