0

Possible Duplicate:
Singleton: How should it be used

why we should use singleton class

Community
  • 1
  • 1
kij
  • 1
  • This is more a philosophical question. There are blenty of Design Pattern books you might read to answer that question. – Yves M. Aug 25 '10 at 10:49
  • http://en.wikipedia.org/wiki/Singleton_pattern – Marko Aug 25 '10 at 10:49
  • Possible Duplicates: http://stackoverflow.com/questions/86582/singleton-how-should-it-be-used http://stackoverflow.com/questions/2080233/is-it-good-programming-to-have-lots-of-singleton-classes-in-a-project http://stackoverflow.com/questions/11831/singletons-good-design-or-a-crutch – Morfildur Aug 25 '10 at 10:50
  • Check these http://stackoverflow.com/questions/tagged/singleton?sort=votes&pagesize=15 – Amr Elgarhy Aug 25 '10 at 10:50
  • Because we have some bad reasons to use it. – nothrow Aug 25 '10 at 10:55
  • This has been answered on numerous occasions on SO. here are a few: http://stackoverflow.com/questions/11831/singletons-good-design-or-a-crutch http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons and my favorite from outside SO giving you multiple reasons to why you shouldn't http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ – Rune FS Aug 25 '10 at 10:59
  • http://en.wikipedia.org/wiki/Singleton_pattern – onder Aug 25 '10 at 10:48

3 Answers3

2

Well, I would try to avoid using them since effectively you are introducing global elements into your project. However you may decide to use them if you have a resource in your project that you only want to ever have one of. For instance a link to a single database source, a cache or a Factory.

Matt_JD
  • 536
  • 4
  • 15
1

don't............

BritishDeveloper
  • 12,481
  • 7
  • 49
  • 59
0

You are building an application in C#. You need a class that has only one instance, and you need to provide a global point of access to the instance. You want to be sure that your solution is efficient and that it takes advantage of the Microsoft .NET common language runtime features. You may also want to make sure that your solution is thread safe.

Benefits

#

The static initialization approach is possible because the .NET Framework explicitly defines how and when static variable initialization occurs. #

The Double-Check Locking idiom described earlier in "Multithreaded Singleton" is implemented correctly in the common language runtime.

Liabilities

If your multithreaded application requires explicit initialization, you have to take precautions to avoid threading issues.

Akyegane
  • 895
  • 6
  • 13