Static Factory vs Constructors

Generally any class allows clients to create objects by providing a public constructor. There is another technique which every programmer should be aware of. An Static Factory. A class can provide one static factory, method  which is simply an static method which returns object of the class.

Note that a static factory method is not the same as the Factory Method pattern from Design Patterns.

There are few advantages and disadvantages of providing static factory methods over constructors. Some of them are listed below.

Unlike constructors, static factory methods have names: –

A static factory with a well-chosen name is easier to use and the resulting client code easier to read. As shown in code below.

public static Boolean valueOf(boolean b) {
       return b ? Boolean.TRUE : Boolean.FALSE;

is better than

public yourClassName(boolean b) {
       return b ? Boolean.TRUE : Boolean.FALSE;

Programmer may provide multiple constructors which varies either in number of params or types or params. The user of such class wont be able to remember both the constructors and they might end up calling wrong one. In cases where a class seems to require multiple constructors with the same signature, replace the constructors with static factory methods and carefully chosen names to highlight their differences.

Static factory methods are not required to create a new object each time they’re invoked: –

Using this, an immutable classes can pre-construct the instances or cache the instances as they’re constructed first time and dispense them repeatedly to avoid creating unnecessary duplicate objects whenever they are asked to do it so.  The  Boolean.valueOf(boolean) method illustrates this technique: it never creates an object.

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. Such classes are called Instance-Controlled classes. Instance control allows a class to guarantee that it is a singleton or non-instantiable.

Static factory methods can return an object of any sub-type of their return type: –

One application of this flexibility is that a method can return objects without making their classes (class names) public. Hiding implementation classes in this fashion leads to a very compact API. This technique lends itself to interface-based frameworks where interfaces provide natural return types for static factory methods.

E.g: –

We have one Collections class which is having below method.

  • synchronizedSet(Set s)

Now imagine, if these method were put in their implementation classes i.e AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet. So you had to remember 8 classes just to get one synchronized set.

Putting this method as static in Collections class, ease job of the user who is going to use this method.

In Static factory method, class of the returned object can vary from call to call as a function of the input parameters: –

Any subtype of the declared return type is permissible. This is self explanatory.

Limitation of providing only static factory methods is that classes without public or protected constructors cannot be subclassed: –

i.e. You can not extend Collections class. Arguably this can be a blessing because it encourages programmers to use composition instead of inheritance and is required for immutable types. It means, its better if you declare Collections as an attribute of any class instead extending it.

Static factory methods is that they are hard for programmers to find: –

Static factory methods doesn’t appear in API documentation as constructors do. So it can be difficult to figure out how to instantiate a class that provides static factory methods instead of constructors.

Hope this had cleared some of your doubt.