Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed. Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, it really has to be a singleton.
In general, I'd say a singleton is slightly neater because it allows you to do some initialization of the singleton object in/from the (private) constructor.
When it is later decided this object should no longer be a singleton (due to new insights or new requirements), it is easier to refactor it: you just need to change all the places that get the instance, instead of all calls to the static methods.
Use static methods, because:
In the specific case of android, you might prefer static methods for performance - I suspect calling a static function might be a bit faster (easier to optimize for the compiler) compared to calling a method on a singleton object.
` I suspect calling a static function might be a bit faster` , Why ?
– Mohammad ErsanSep 7 '11 at 7:31
1
Basically, because 'Singleton' is a pattern instead of something embedded in the Java language, the JVM will have to execute the 'calling a method on an object' logic/infrastructure (looking up exactly which code should be called because the method might have been overridden etc). With calls to static functions, it can always skip all that, and probably to some more optimizations because it can make some more/easier assumptions. Some of this might be 'optimized away', but afaik in android you still have to do much of the optimization yourself in the code.
– Arnout EngelenSep 7 '11 at 7:52
It depends. Singleton concept considers a limitation on object initialization. In other words, singleton object must be the only instance of a singleton class in the runtime. But if you just need to create objects of some family of classes, then go with factory method pattern.
I'd go for the Singleton because it allows you to use Java's object-oriented features like inheritance (method overriding) which won't work for static methods.
It doesn't really help with inheritance.
– TomSep 7 '11 at 6:45
@Tom So you argue that Factory.createLoginRequest() could have the same context-dependent dynamic behaviour as Factory.getInstance()?
– Frank GrimmSep 7 '11 at 6:50
Yes, you'd just use inheritance behind the simpler API.
– TomSep 7 '11 at 7:14
2
in singleton you cant extend the class, because the constructor is private, so you cant have another object than the instance the class provide.
– Mohammad ErsanSep 7 '11 at 7:26
1
Actually you can make a Singleton class extendable if you want to. You simply make the constructor to be protected instead of private and then you can make the subclass with a protected constructor as well (or a private constructor if it is a final class). The subclass then has its own getInstance() method that overrides the getInstance() method of the superclass.
– Danny Remington - OMSSep 4 '13 at 15:36