Learn
4 min readOct 3, 2020

--

Java — Consider static factory methods
When you want to provide ability to create instances of your class, the first instinct is to provide constructors. Also consider static factory methods as these have several advantages as discussed below.

Static factory methods have names
Constructor functions need to have the same name as the class name which means that you as the class designer have no way of providing a descriptive name for the constructor. Static factory methods lets you give meaningful names to your construction methods allowing the consumers of your class to have a better experience.

Choice of whether to create the object or not
The static factory method has the choice of reusing an existing instance or creating a new instance based on the state of the system. Constructors have no such choice and would always be expected to create an instance.

Instance-Controlled classes
The above choice of having a static factory method which can create a new instance as required or return an existing instance allows your class to have control over how many instances of your class gets created. Such classes are referred to as instance-controlled classes.

Use cases of instance-control
Singleton
Instance-control technique can be used to ensure that a class is a singleton.

Non Instantiable
A class with a bunch of static methods have no reason to be instantiated.
Such a class can be made non-instantiable by making it’s constructor private.

Equal instances = Same instances
Instance control can be used to ensure that only one instance gets created for one input data. In other words, multiple semantically equal instances cannot be created.

a.equals(b) implies that a==b.

Flyweight pattern and equal instances
Flyweight pattern is about controlling the creation of huge objects by reusing existing ones. This is again done using instance-control by the class.

Can return subtype instead of the main type
With static factory method, the class has the flexibility to return instance of any of the sub classes based on the argument data that came in. With constructors, the class has to return an instance of the particular class itself and cannot return an instance of the subclass.

Static factory method’s ability to return instances of sub-type and hidden sub-classes
Since the factory method can return instances of sub-types, these sub-types can actually be hidden as well.When the sub-type does not provide any additional API but is needed only from an implementation standpoint, it makes sense to have sub-types hidden instead of polluting the name space with new classes which were required only from an implementation standpoint and do not add any value to the clients.

Interface-based frameworks and static factory method’s ability to return instance of a sub-class
Interface-based framework is a framework that is based on an interface. Multiple implementations of these interface are done and based on what arguments are passed to the static factory method, the implementation class that is actually created would differ but the return type would be the interface type.

Static factory method can not only return instances of sub-classes but can also have the same static method return instances of different sub-classes
This has already been touched on earlier. To reiterate, you can have one class have two static factory methods — one returning instance of subClassOne and another returning instance of subClassTwo. You can also have the same method returning instance of subClassOne and instance of subClassTwo based on the nature of the argument values passed into the method.

EnumSet as an example of a class with a static factory method that can return instances of different classes
Static factory method in EnumSet can return either an instance of the implementation class RegularEnumSet or an instance of the implementation class JumboEnumSet based on whether the enum set had less that sixty four members or more than sixty four members.

Disadvantages of static factory methods
The main disadvantages of static factory methods is that a class without a public or protected constructor cannot be sub-classed. Also, they are a little hard to find as they don’t stand out as well as a constructor.

Common names for static factory methods

from (Type conversion)
A type-conversion method which converts from the argument that is passed in to the particular type in which the method is present.

e.g. Date d = Date.from(instant);

of(Aggregation)
Aggregation method to create a bunch of instances.

e.g. Set<String> colors = Set.of(“Red”,”Green”,”Blue”);

valueOf
Verbose alternative to “of” and “from”.

BigInteger bigInteger = BigInteger.valueOf(Integer.MAX_VALUE);

instance or getInstance
Return an instance of the particular type as described by the passed in argument.

StackWalker stackWalker = StackWalker.getInstance(options);

create or newInstance
Similar to instance/getInstance except that this method would ensure that a new instance would always get created. The argument describes what kind of instance needs to be created.

Object newObject =Array.newInstance(classObject,arrayLength);

get<Type>
Used when the type returned is not the same as the class that contains the static method. Takes an argument that acts as the specification just like “getInstance”, but the type that is created is not this particular class. Hence, the type is being specified in the method name.

e.g FileStore fs = Files.getFileStore(path);

new<Type>
new<Type> is similar to “newInstance” in that it ensures that a new instance is created but additionally indicates that the instance created would be of type <Type>

newInstance:getInstance::new<Type>:get<Type>

type
Concise alternative to “get<Type>” and “new<Type>

e.g Collections.list

The static factory method name conventions are as below
from,of
instance/getInstance::create/newInstance::get<Type>:new<Type>

--

--