3

I want to know if both singleton and static factory method create only one instance then why there are two concepts for same purpose?

Note: Here term "static factory method" is taken from Effective java book by Joshua bloch where he has written:

"A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked. This allows immutable classes (Item 15) to use preconstructed instances, or to cache instances as they’re constructed, and dispense them repeatedly to avoid creating unnecessary duplicate objects. The Boolean.valueOf(boolean) method illus- trates this technique: it never creates an object. This technique is similar to the Flyweight pattern [Gamma95, p. 195]. It can greatly improve performance if equivalent objects are requested often, especially if they are expensive to create. The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time. Classes that do this are said to be instance-controlled. There are several reasons to write instance-controlled classes. Instance control allows a class to guarantee that it is a singleton (Item 3) or noninstantiable (Item 4)"

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • 2
    Singleton ensures only one instance exists, static factory method is just an alternative to using a constructor in some cases, usually because the object is complex to create, but does not limit to a single instance. – d.j.brown Oct 22 '17 at 16:56
  • 1
    Static factory method encapsulates object creation, you can still create multiple objects. – Pratyay Oct 22 '17 at 16:56
  • Answer is they don't, factory method is a replacement for a list of constructors. If you pass the same arguments to the factory method you will give a different object every time. – 11thdimension Oct 22 '17 at 16:59
  • "static factory method create[s] only one instance" this is where you are misunderstanding. It might help if you show what you mean by a static factory method, because you may be using that name to refer to something different. – Andy Turner Oct 22 '17 at 17:03
  • @AndyTurner static factory method mentioned here refers to: public static HashMap newInstance() { return new HashMap(); } – himanshu tripathi Oct 22 '17 at 17:29
  • @himanshu yep, that's what I understand by a static factory method too. What makes you think that only creates a single instance? It will create a new instance each time it is invoked. – Andy Turner Oct 22 '17 at 17:30
  • @Pratyay , Kindly explain how can we create multiple objects, I am still not able to get it. Thanks a lot – himanshu tripathi Oct 22 '17 at 17:32
  • @AndyTurner, Sir then I am confuse with the line "The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time", from Effective Java book. – himanshu tripathi Oct 22 '17 at 17:36
  • 2
    @himanshu "they are **not required** to create a new object each time". They can create a new instance, if so desired, and the example you show does. – Andy Turner Oct 22 '17 at 17:45
  • 1
    @AndyTurner oh I got it, that was the confusion. Thank you so much sir for prompt reply. – himanshu tripathi Oct 22 '17 at 17:56

2 Answers2

2

The point being made by the line "they are not required to create a new object each time they’re invoked" is that (unlike new, which always creates a new object) a factory method could be implemented in some more clever way that reuses an existing object.

Take a look at the of() factory method in Guava's ImmutableList. This method returns "the" empty immutable list - there's no point constructing a new object every time of() is called, instead the same object is always returned. This is safe because any empty ImmutableList instance is functionally indistinguishable from any other empty instance.

dimo414
  • 47,227
  • 18
  • 148
  • 244
1

A static factory method is a means of construction, it may return new instances, alternate subclasses of a type, wrap critical logging or registry, compose a number of items into an object, or (maybe) return back a single static instance.

A singleton obtained by any means always resolves back to the same instance. This means there is no variability.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Thanks for your reply and Till now I got it that static factory methods " may 'also' return new instances" unlike Singletons. But it would help me even more if you please elaborate. In which case static factory methods may return new object and in which case not?. (By reading effective java it seems it just create single object as I mentioned a para from Effective java in my question and concept is unclear scenario wise) – himanshu tripathi Oct 24 '17 at 18:50
  • Whenever it makes sense in the design. Possibly you don't want to share the response in a different thread. You could return an object that represents something in hardware instead of in memory. Logging speaks for itself, you cannot log when directly referencing a static class field - without code injection anyway. – Maarten Bodewes Jan 27 '18 at 20:00