Design patterns: Factory method naming conventions

Ibnahmad Mohamed
1 min readApr 5, 2023

--

Factory method pattern naming conventions The “factory method” is a very useful pattern, but it has som limitations when it comes to documentation. Constructors “stand out” in API documentation and are easy to spot, but factory methods on the other hand can be hard to find. There are some naming conventions laid out in effective java that can reduce this problem and make the factory methods easier to find in an IDE.

from

Type-conversion. Takes a single parameter and returns an instance of the same type as the parameter. Date d2 = Date.from(d1);

of

An aggregation method that takes multiple parameters and returns an instance that “incorporate” the parameters. Return type is same as parameters. Set<Gender> genders = EnumSet.of(MALE, FEMALE);

valueOf

More verbose alternative to from and of BigInteger.valueOf(Integer.MAX_VALUE);

instance or getInstance

Returns an instance that is described by its parameters (if any). Doesn’t have to have the same type as parameters. Return value is typically cached (singleton, etc) KeyFactory.getInstance("RSA");

create or newInstance

Like instance or getInstance but returns a new instance on each invocation Array.newInstance(classObject, length);

getType or type

Like getInstance, but used if the factory method in in a different class than the type. Collections.list();

--

--