The Singleton Design Pattern

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
20,468
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Apr 2, 2007

An explanation of the Singleton design pattern and its purpose.

Great wikipedia entry:
http://en.wikipedia.org/wiki/Singleton_pattern

And this one which I now think is inappropriate, but which I mentioned in the video:
http://www-128.ibm.com/developerworks/library/j-dcl.html

Category:

Howto & Style

Tags:

License:

Standard YouTube License

  • likes, 16 dislikes

Link to this comment:

Share to:

Uploader Comments (codingkriggs)

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

  • You forgot to talk about the singleton in a multi-threaded environment. If you don't do it properly, you will get multiple instances!

  • 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 - ....(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 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.

see all

All Comments (28)

Sign In or Sign Up now to post a comment!
  • 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...
Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more