Advantage of Static class over use of Singleton

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Duplicate

http://stackoverflow.com/questions/86654/whats-wrong-with-singleton http://stackoverflow.com/questions/86654/whats-wrong-with-singleton http://stackoverflow.com/questions/11831/singletons-good-design-or-a-crutch http://stackoverflow.com/questions/11831/singletons-good-design-or-a-crutch http://stackoverflow.com/questions/86582/singleton-how-should-it-be-used http://stackoverflow.com/questions/86582/singleton-how-should-it-be-used http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons

You can find numerous reasons for using a Singleton over a Static class. But there must surely be some situations where it is better to use a static class before a Singleton. What are they?

Answers

You can use static class when:

1) all its methods are utilities (nice example - class Math)

2) you don t want to deal with preserving your instance from garbage collector (in applets), but I would better use singleton there

3) you are absolutely sure that it wouldn t become stateful in the future and you are sure that you will always need only one instance of that class

If you are using singleton and in one moment you realize that you need several instances then your singleton easily can be transformed to multitone, but you ll have a problem with static class

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/839383/advantage-of-static-class-over-use-of-singleton

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils