DataInTheStone
DataInTheStone

Reputation: 57

Why utilize static methods and a static singleton instance when utlizing the singleton design pattern?

I am learning the singleton pattern and saw that you are to use the static keyword when creating a singleton instance inside the class and static methods when utilizing the singleton instance. What is the point of this when the private constructor prevents you from instantiating new singleton objects? The methods would only be usable by one object anyways.

class Singleton
{
    private static Singleton instance;
    private Singleton() {}                                                 
    public static Singleton getInstance()
    {
        if(instance == null)
            instance = new Singleton();
            return instance;
    }
}

Wouldn't I need just a private constructor to create a fully functional singleton pattern?

Upvotes: 0

Views: 41

Answers (2)

T.S.
T.S.

Reputation: 19384

"...static methods when utilizing the singleton instance. What is the point of this when the private constructor prevents you from instantiating new singleton objects?"

The whole idea of the singleton - abstract creation, provide static access, make sure there is only one instance, state.

So, when you say "prevents you from instantiating new singleton objects" - that is the goal.The goal of singleton is to have a single instance of which you have no control.

in c#/.net one of the correct operations would be this, and this implementation requires no explicit locking


class Singleton
{
    private static Singleton instance;

    static Singleton() // executes once per app run, initializes singleton
    {
        instance = new Singleton();
        instance.InitializeSingletonUsingThisPrivateMethod();
    }                                                 
 
    private Singleton() 
    {
       // hides construction to singleton. 
       // Used to init single instance
    }                                                 


    public static Singleton Instance { get; } => instance
    

}

Classic method thread-safe singleton is this

class Singleton
{
    private static Singleton _instance;
    private static object _lock = new object();
                                                
 
    private Singleton() 
    {
       
       InitializeSingletonUsingThisPrivateMethod();
    }                                                 


    public static Singleton Instance 
    {
        get
        {
            if (_instance == null)
            {    
            
                lock(_lock)
                {
                    if (_instance == null)
                        _instance = new Singleton();
                }
            }
            return _instance;
        }
    }   

}

Specifically for .net, there are few more ways to create singleton.

Upvotes: 1

ddfra
ddfra

Reputation: 2565

Having only a private constructor, not supported by a static getInstance method, wouldn't allow any one to get any instance.

Note that this implementation of the singleton pattern is not the "most correct" as it is not thread safe.

The easiest way to implement a Singleton in Java (and in most of OOP languages) is to use an enum with a single instance.

It is even possible to get singletons by dependency injection

Upvotes: 1

Related Questions