factory.jpeg

1. Clear, proper and descriptive names

A well-chosen name is easier to use and the resulting client code is easier to read. A class can only have a constructor with the same signature. Static factory methods don’t have this restriction, choose names that properly highlight their differences.

2. Not required to create a new instance for every invocation

Immutable and final classes can make use of pre-constructed instances, or to cache instances as they are constructed. These instances are returned to client code to avoid creating unnecessary duplicate objects. This technique is similar to the flyweight pattern.

3. Able to return objects of any subtype of their return type

This gives you the flexibility in deciding the class of the returned object. The client refer to the returned object by interface rather than the implementation class. Any subtype of the declared returned type is permissible. The class of the returned object can vary from release to release, clients neither know nor care about the class of the object they get from the factory.