By kirillcool
via dailydevelopment.blogspot.com
Published: Aug 02 2007 / 03:43
There are a lot of posts about that the Singleton design pattern is not pattern but anti-pattern. Some posts also propose alternatives for Singleton design pattern or solutions for already existing Singleton design pattern problem. But I haven't found any post which describes solution for big project with bunch of singleton class usage without big effort of rewriting many places by hand.
Comments
warpedjavaguy replied ago:
The motive is good :)
wolfdancer replied ago:
finally a post talks about what should be talked about regarding singlton
willcode4beer replied ago:
Wasn't one of the "features" of the singleton design pattern that you can replace:
public static MyBigObject getInstance() {
return INSTANCE;
}
with:
public static MyBigObject getInstance() {
return new MyBigObject();
}
to change away from the singleton (without changing any other code).
I guess that's really the factory pattern part of a singleton.
Maybe the anti-singleton rhetoric is really pro-factory
;-)
spand replied ago:
I dont know about other people but I used (and saw it as a feature of singletons) == for equality on singletons quite a lot for some time. So if some people are like me, which I am quite sure there are, then it wont be just that easy. Maybe this is one of the reasons its considered an antipattern..
in86835 replied ago:
So naive, the bloke has no real clue why Singletons exist or how to use them properly, his 'solution' looks a lot worse than a Singleton, especially for multi-threaded code.
willcode4beer:
No factories are not the same as Singletons. Factories can have an interface and different implementations or instances. and only provide objects which are instances of a class or interface. Singletons have at most one instance per context and can receive or return primitive or object values, they can act like factories, but not all factories are Singletons..
Enum objects are Singletons too!
Voters For This Link (18)
Voters Against This Link (3)