P
Philipp
Hello,
I've been reading a little about the difference between using a
singleton pattern vs using a static class (ie all methods are static,
ctor is private, state is in static vars).
A lot of blogs show that there are differences between these two
approaches. I can't understand most of them. Any help would be appreciated.
---
Assertion found on web: "The Singleton pattern is useful when the object
itself needs to keep some state. If no such thing is needed, a static
method can do the job as well."
My POV: No, you can put a state in a static var and access it through
static methods.
---
Flexibility of the factory mehod: You can simply create two subclasses,
MyDevelopmentSingleton and MyProductionSingleton, and let your
MySingleton.getinstance() function decide which to return.
My POV: If you declare you ctor private to enforce the singleton
pattern, you cannot subclass it. If you declare the ctor protected, is
it still a true singleton? Users can then create more than one object...
---
Assertion found on web: The main difference between static and Singleton
classes is that static classes don’t ever get initialised.
My POV: I don't understand. What about static initializers?
---
So, in the end it's not clear for me why one should prefer the Singleton
pattern over the static class (should I?). I mean
SingletonClass.getInstance().setSomething(int a);
SingletonClass.getInstance().getSomething();
is quite quivalent to
StaticClass.setSomething(int a);
StaticClass.getSomething();
Any comments are welcome. Best regards
Phil
I've been reading a little about the difference between using a
singleton pattern vs using a static class (ie all methods are static,
ctor is private, state is in static vars).
A lot of blogs show that there are differences between these two
approaches. I can't understand most of them. Any help would be appreciated.
---
Assertion found on web: "The Singleton pattern is useful when the object
itself needs to keep some state. If no such thing is needed, a static
method can do the job as well."
My POV: No, you can put a state in a static var and access it through
static methods.
---
Flexibility of the factory mehod: You can simply create two subclasses,
MyDevelopmentSingleton and MyProductionSingleton, and let your
MySingleton.getinstance() function decide which to return.
My POV: If you declare you ctor private to enforce the singleton
pattern, you cannot subclass it. If you declare the ctor protected, is
it still a true singleton? Users can then create more than one object...
---
Assertion found on web: The main difference between static and Singleton
classes is that static classes don’t ever get initialised.
My POV: I don't understand. What about static initializers?
---
So, in the end it's not clear for me why one should prefer the Singleton
pattern over the static class (should I?). I mean
SingletonClass.getInstance().setSomething(int a);
SingletonClass.getInstance().getSomething();
is quite quivalent to
StaticClass.setSomething(int a);
StaticClass.getSomething();
Any comments are welcome. Best regards
Phil