3 ms·
Not all singletons are "global state". Yes, I agree that using singletons to store global state is a bit dumb. You may as well use static methods and variables
by mnw21cam 13y ago
Not all singletons are "global state".
Yes, I agree that using singletons to store global state is a bit dumb. You may as well use static methods and variables.
Where a singleton does become useful is as an immutable carrier of methods. No state.
It means that an object somewhere can have a reference to a WeirdOperator object, which contains a method called doWeirdOperation(inputs). WeirdOperator would then be an interface or abstract class, and various different operators are implementations, thus effectively allowing methods to be passed around. Consider Comparator (in Java).
The alternative is to access methods by introspection, but doing Method.invoke(parameters) is nasty and evil and slightly slower than doing it properly.
- peeters 13y agoWhen classes have no state, there's also really no need to have a singleton instance. If someone wants to instantiate a new object, who cares? It's a few extra cycles and a few more bytes in memory. Sure, you can have an easily accessible instance for convenience and performance, but nothing's going to hurt if somebody uses a different instance. Your program isn't compromised. Singleton exists as a pattern to enforce a single instance because of state and side effects. What you're talking about is just a cached instance.
- masklinn 13y ago> Not all singletons are "global state". Then why have a singleton? > Where a singleton does become useful is as an immutable carrier of methods. No state. Why have a singleton? Just use functions, or static/class methods in KON languages.
- mnw21cam 13y agoCertain languages don't have methods as first class objects which can be passed around. In Java, you can pass around a Method object, but it isn't really the full thing, as calling it is not type safe, and is slower than a real method call. Likewise, passing around a Class object in order to identify a static method to use is equally unwieldy. Therefore, a singleton object can be passed around, and it carries with it the implementation of the method. That allows you to have a variable that effectively references a type-safe fast method, allowing the nearest thing to having methods as first class objects.
- masklinn 13y ago1. so you're not only using singletons but you're using them in horrible ways, that's just great 2. to pass your singleton around, you need to know its type. If you know its type, you can just bolt the methods on the type itself and get rid of the useless singleton
- mnw21cam 13y agoNo, you don't need to know the singleton's type. You can have several implementations of an interface, each of which is a singleton. Then you have a reference to the interface type, which allows different methods to be selected.
- masklinn 13y agoAh yes, the fabled multisingleton, because once you've dug your hole there's nothing better than filling it with raw sewage, and once you've got a singleton you can fill it up with half a dozen interfaces because reasons.
- brown9-2 13y agoYour example isn't really a Singleton. Comparator is best understood as the strategy pattern.