How not to create an instance of a singleton
-
I came across this java code. (I changed the class name to SingletonClass)
private static SingletonClass instance;
public SingletonClass()
{
SingletonClass.instance = this;
}public static SingletonClass getInstance()
{
return instance;
}Singleton... public constructor... I don't see anything wrong here :rolleyes: Oh... And I'm curious what you do when you don't call the constructor at least once :laugh:
-
I came across this java code. (I changed the class name to SingletonClass)
private static SingletonClass instance;
public SingletonClass()
{
SingletonClass.instance = this;
}public static SingletonClass getInstance()
{
return instance;
}Which of course is used like this:
SingletonClass myVerySingletonClass = (new SingletonClass()).getInstance();
or even better:SingletonClass anInstanceThatWeDontUse = new SingletoneClass();
SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();Good stuff! :laugh: On the other hand, I actually have written code similar to this, except I don't call it a singleton of course. Granted, it's smelly code. Quiz: What might be the reason to provide a static method that returns an instance of the class, when you know that there will ever only be one instance? Marc
-
I came across this java code. (I changed the class name to SingletonClass)
private static SingletonClass instance;
public SingletonClass()
{
SingletonClass.instance = this;
}public static SingletonClass getInstance()
{
return instance;
}That's just an example of the well-known Renewable Singleton Pattern. That comes in very handy when the old instance has worn out. For a closer description, see
"Real World Software Development, Vol. II: Design Patterns", by W.T. and F. (2021)
-
Which of course is used like this:
SingletonClass myVerySingletonClass = (new SingletonClass()).getInstance();
or even better:SingletonClass anInstanceThatWeDontUse = new SingletoneClass();
SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();Good stuff! :laugh: On the other hand, I actually have written code similar to this, except I don't call it a singleton of course. Granted, it's smelly code. Quiz: What might be the reason to provide a static method that returns an instance of the class, when you know that there will ever only be one instance? Marc
SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();
This would actually generate a compile error as it
getInstance()
is a static method ;P -
SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();
This would actually generate a compile error as it
getInstance()
is a static method ;PNicholas Marty wrote:
This would actually generate a compile error as it
getInstance()
is a static method:doh: Marc
-
That's just an example of the well-known Renewable Singleton Pattern. That comes in very handy when the old instance has worn out. For a closer description, see
"Real World Software Development, Vol. II: Design Patterns", by W.T. and F. (2021)
Ah yes! Code Entropy!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
-
Which of course is used like this:
SingletonClass myVerySingletonClass = (new SingletonClass()).getInstance();
or even better:SingletonClass anInstanceThatWeDontUse = new SingletoneClass();
SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();Good stuff! :laugh: On the other hand, I actually have written code similar to this, except I don't call it a singleton of course. Granted, it's smelly code. Quiz: What might be the reason to provide a static method that returns an instance of the class, when you know that there will ever only be one instance? Marc
If there'll only ever be one instance, that's your typical singleton, isn't it? I don't really understand your quiz question. I've done load-on-demand singletons in the past which may be what the doofus in charge of this example was going for, I suppose.
-
SingletonClass useThisInstead = anInstanceThatWeDontUse.getInstance();
This would actually generate a compile error as it
getInstance()
is a static method ;P -
Actually, in Java you are allowed to call a static method from an instance... One of many dumb things Java does.
Uh... Somehow I mistook the code for c#... Somehow skipped that in the original post :~ At least in c# it isn't possible to do that and I'd think even Java should at least give a compiler warning about that ;)
-
Ah yes! Code Entropy!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)