0

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?

T.S.
19.6k11 gold badges69 silver badges96 bronze badges
asked Jul 26, 2022 at 20:26

2 Answers 2

1

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

answered Aug 1, 2022 at 21:50

Comments

1

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

answered Jul 27, 2022 at 0:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.