The static factory method pattern is an object creational pattern that makes use of a static method to create an instance
of the class, in combination with (usually) a private constructor for the class. Thus, the object creation is controlled
through these factory methods.
Thus, the static factory method is different from the Factory method
and Abstract factory
patterns described in the
classic Gang of Four book.
Joshua Boch in Effective Java
has one of the
best descriptions of this design pattern. Much of the wisdom in this article is derived from Effective Java
.
Next, we are going to see the main highlights of the static factory methods, and examples illustrating the highlights from the JDK.
Main highlights of the static factory method:
-
They have descriptive names, improving readability.
Consider methods such as these ones in
java.time.LocalDate
:// Current date from the system clock in the default time-zone public static LocalDate now() { ... } // Current date from the system clock in the supplied time-zone public static LocalDate now(ZoneId zone) { ... }
-
They can have the same signature and different names, unlike constructors.
Two methods to illustrate this can be found in
java.util.Collections
:public static <T> Collection<T> synchronizedCollection(Collection<T> c) public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
-
They can return any subtype of the return type.
You can see this highlight in action in
java.util.Collections
in methodunmodifiableCollection
:// Returns a read-only version of the collection. The UnmodifableCollection is a public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) { return new UnmodifiableCollection<>(c); }
-
They don’t need to return a new object each time they are invoked.
This is nicely illustrated in
java.util.Boolean
, in methodvalueOf
:// Returns one of the two static final instances of Boolean // that correspond to the true or false primitives. public static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); }
Main disadvntages:
- Classes with non-public and non-proteted constructors cannot be subclassed.
- The factory methods do not stand out in the documentation of the API.
As a side note, looking at the main highlights of the static factory method, I see a some similarities with smart constructors as used in Scala.