When would a singleton actually be easier or better than a static class? It seems to me creating a singleton is just extra effort that's not actually needed, but I'm sure there is a good reason. Otherwise, they wouldn't be used, obviously.
相关问题
- suppress a singleton constructor in java with powe
- What uses more memory in c++? An 2 ints or 2 funct
- How Does WebSphere Choose the Classloading Order i
- Store data and global variables using the Applicat
- PHP Adding stylesheets to header
相关文章
- Private static variables in php class
- NameError: name 'self' is not defined, eve
- How to fix a purported lack of an “explicit instan
- Passing static array in attribute
- Java “static import” vs. “import static” in Java 8
- Auto-property initializer Singleton implementation
- .NET - how to make a class such that only one othe
- Why does Java's List have “List.toArray()”, bu
Singletons are often preferred to global variables because:
Source
EDIT:
One cool use of the singleton is, when combined with the factory method, can be used to create the Flyweight pattern. This is when you create a new object, the Factory (instead of creating a new object) first checks to see a singleton of that object is already made, if it is, it just returns that object, if not, it creates a new singleton and returns that, keeping track of the singletons it creates. Flyweights work because of the immutability of the singleton.
One good reason for preferring a singleton over a static class (assuming you have no better patterns at your disposal ;) ), is swapping out one singleton instance with another.
For example, if I have a logging class like this:
It works really well, but what if Logger writes things to a database, or writes output to the console. If you're writing tests, you probably don't want all those side-effects -- but since the log method is static, you can't do anything except.
Ok, so I want to hot-swap my Log method for testing. Go go gadget singleton!
So you can define a
TestLogger : Logger
with an emptyLog
method, then set an instance of your test logger to the singleton instance for tests. Presto! You can hotswap your logger implementation for tests or production without affecting client code.In many languages static classes lack useful features, like inheritance (and polymorphism more generally).
(Not that I'm advocating singletons.)
Singletons preserve the conventional class approach, and don't require that you use the static keyword everywhere. They may be more demanding to implement at first, but will greatly simplify the architecture of your program. Unlike static classes, we can use singletons as parameters or objects.
Also,you can use singletons with interfaces just like any other class.
Singletons always seemed a bit redundant to me. I prefer static classes, and if I need different behavior, I combine it with a dependency injection and a provider.. I don't know what pattern this is, or if it has a name, but it usually goes something like this:
Then I just make sure to set the provider somewhere in my init method. It's easy enough to add lazy initialization if you want to by setting the default provider at class initialization. Best of all, it allows you to change the behavior while still getting the aesthetic of using static classes.