The Singleton Design Pattern
Loading...
20,468
Loading...
Uploader Comments (codingkriggs)
see all
All Comments (28)
-
Singleton is EVIL PATTERN!
-
@zck7 He didn't give it a new name...
-
@Thetranslatorguy I take it you're being purposefully confusing.
-
@codingkriggs I still am unable to get why would you need a Singleton when you can do the same thing with any class with all the static methods and with a private constructor meaning that no instantiation is possible. Example: public class ConPoolFactory{ private map pool = ... static{ //init } private ConPoolFactory public static Connection getConnection(){ } } And if you want instance's properties, you can grab them with public static getter methods. What can be wrong with this?..to be cont
-
Singletons are an anti-pattern; a recurring solution that while superficially attractive usually leads to worse design.
Singletons frequently make testing a head ache and compromise both the modularity and the 'modelarity' of the code(the degree of correspondence between the components of the problem being modelled and the components of its solution), harm encapsulation, make code difficult to evolve and extend.
The PfA pattern is often a good anti-dote to singletons.
-
oh my god! wtf? this is a waste of time.
Loading...
I and others always called this a one-time-only initialization. I don't see the need for giving it a new name "Singleton".
zck7 7 months ago
@zck7 I'll fill out my reply from the Observer video. The whole idea about a pattern is to identify components which are often found together to solve a certain problem. Many would argue that a single person does not invent a pattern, but recognizes its use in many places. So it is very common for programmers of different languages to come up with a same solution, but each with different names. In the case of the Observer, no name could have made everyone happy because many already existed.
codingkriggs 7 months ago
You forgot to talk about the singleton in a multi-threaded environment. If you don't do it properly, you will get multiple instances!
gamccoy 4 years ago
Yes, I indeed didn't mention that. However, this is an introduction for those who haven't yet been introduced to design patterns, so I'm keeping it simple. The wikipedia entry I refer to has a wide range of solutions for all kinds of languages, including a few multi-thread safe ones for java. I hope you find what you need there.
codingkriggs 4 years ago
@codingkriggs - ....(cont).. and if it can work fine, then WHERE is the usage of ONE OFF instantiation.
I am confused.
Thanks for the video by the way.
getalifein 10 months ago
@getalifein My favourite reason for singleton is extensibility through polymorphism. I can have an interface IManager, then a default implementation ManagerImpl, with a single instance accessed through ManagerImpl.getInstance(), for example, and then pass that around to other methods as a parameter. Another developer could implement their own AlternateManager implementing IManager, and so the called method would be unaware which implementation they're receiving, improving extensibility.
codingkriggs 10 months ago