15

Possible Duplicate:
Difference between static class and singleton pattern?

Which is better in Java,

implementing public static methods, like

Factory.createLoginRequest()

or implementing Singleton pattern, like

Factory.getInstance().createLoginRequest()

(Boths will return a Request object.)

Which one is better and why ?

Community
  • 1
  • 1
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
  • 1
    Depends on the circumstances. Without more detail, this question is not answerable objectively. – Mat Sep 07 '11 at 06:42
  • 1
    By the way, these days the hip way to implement a singleton in Java is using an enum, see http://stackoverflow.com/questions/70689/efficient-way-to-implement-singleton-pattern-in-java – Arnout Engelen Sep 07 '11 at 06:53

4 Answers4

7

It depends.

Choose singletons, because:

  • 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.
Arnout Engelen
  • 6,709
  • 1
  • 25
  • 36
  • ` I suspect calling a static function might be a bit faster` , Why ? – Mohammad Ersan Sep 07 '11 at 07: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 Engelen Sep 07 '11 at 07:52
7

from wikipedia:

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.

Mahdi Hijazi
  • 4,424
  • 3
  • 24
  • 29
0

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.

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34
0

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.

Frank Grimm
  • 1,151
  • 7
  • 11
  • It doesn't really help with inheritance. – Tom Sep 07 '11 at 06:45
  • @Tom So you argue that `Factory.createLoginRequest()` could have the same context-dependent dynamic behaviour as `Factory.getInstance()`? – Frank Grimm Sep 07 '11 at 06:50
  • Yes, you'd just use inheritance behind the simpler API. – Tom Sep 07 '11 at 07: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 Ersan Sep 07 '11 at 07: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 - OMS Sep 04 '13 at 15:36