Answer by gnasher729 for Alternatives to the singleton pattern
It depends very much what you mean by singleton. My definition: A “singleton instance” (not class) is an object that serves a specific purpose, that should be exclusively used for that purpose, that is...
View ArticleAnswer by Winston Ewert for Alternatives to the singleton pattern
There are a few alternatives:Dependency InjectionEvery object has its dependencies passed to it when it was created. Typically either a framework or a small number of factory classes are responsible...
View ArticleAnswer by Telastyn for Alternatives to the singleton pattern
Your original requirements:So the general my scenario is thatI need only one instance of a class either for optimization reasons (I do not need >multiple factory objects) or for sharing common state...
View ArticleAnswer by CodeART for Alternatives to the singleton pattern
How about using IoC container? With some thought you can end up with something on the lines of:Dependency.Resolve<IFoo>(); You would have to specify which implementation of IFoo to use at a...
View ArticleAnswer by Gort the Robot for Alternatives to the singleton pattern
If you are using a multiparadigm language like C++ or Python, one alternative to a singleton class is a set of functions/variables wrapped in a namespace. Conceptually speaking, a C++ file with free...
View ArticleAnswer by umlcat for Alternatives to the singleton pattern
Singletons EncapsulationCase scenario. Your application is Object Oriented, and requires 3 or 4 specific singletons, even if you have more classes.Before Example (C++ like pseudocode):// network...
View ArticleAnswer by Michael Borgwardt for Alternatives to the singleton pattern
Most people (including you) completely misunderstand what the Singleton pattern actually is. The Singleton pattern only means that one instance of a class exist and there is some mechanism for code all...
View ArticleAnswer by Caleb for Alternatives to the singleton pattern
Your second option is a fine way to go -- it's a kind of dependency injection, which is the pattern used to share state across your program when you want to avoid singletons and global variables.You...
View ArticleAnswer by tdammers for Alternatives to the singleton pattern
The purpose of a singleton is to enforce that only one instance can ever exists within a certain realm. This means that a singleton is useful if you have strong reasons to enforce singleton behavior;...
View ArticleAnswer by Randall Cook for Alternatives to the singleton pattern
If your needs from the singleton can be boiled down to a single function, why not just use a simple factory function? Global functions (probably a static method of class F in your example) are...
View ArticleAlternatives to the singleton pattern
I have read different opinions about the singleton pattern.Some maintain that it should be avoided at all costs and othersthat it can be be useful in certain situations.One situation in which I use...
View Article