Thursday November 26th

Wednesday November 25th

Design Patterns: Thoughts on the Singleton Pattern

Thoughts on the Singleton Pattern (Executive summary: Don't!)


"Don't use a singleton."
"They're pretty much the same as global variables and static classes. Use those instead."
"Well, if they're pretty much the same, then why not use a singleton?"

Also: Author notes he fixed some spelling and "grammer" problems.

For extra lulz, here's the author and submitter:

Actually, that isn't me, but a friend of mine (I took the picture)

And I do say why to not use a singleton: " If you are going to have to deal with all those problems anyway, you might as well choose the method which is both the most easily understood, most efficient, and requires the least typing."

I'm using a singleton as a memory store for a collection of objects that are constructed from the database.
This is used in a web app. The singleton pattern provides a nice way to store a singular set of data that is loaded from the DB once without having to continually hit the DB for accessing the same information.

Singleton has a purpose and it serves itself well in the web app model.

Unless you are using your singleton with great care in the web app area, you will find that it does anything but serve well.

think twice before you opt for singleton. And if after you thought twice you still mean to use it - make it bullet-proof. Then come back and tell us if the effort was worth it to use the pattern.

Commenting on Stories is limited for now and will open up to those recommended by the community. Learn how
Loading DotNetKicks...
brought to you by the Kicks Network