Warning: Use of undefined constant fixed - assumed 'fixed' (this will throw an Error in a future version of PHP) in /home/webtro13/domains/farenda.com/public_html/wp-content/themes/moments/header.php on line 65
class="header_container_flat">

Static Factory Method is very popular pattern that simplify and clarify creation of objects. Can be used instead of or in addition to constructors.

Like everything else, Static Factory Methods have their pros and cons. Therefore we’ll go through them with concrete examples.

Name that clarifies intent

Good names makes the code easier to understand, hence less error prone and therefore faster to develop. I like to code fast. :-) Intention revealing name is one of those small ingredients of clean code that speed up development.

For example we’ve got the following class that can be instantiated from the given string, which is expected to be a date in ISO format:

Of course the solution is not perfect, but at least it tells us what it expects and gives us a chance to spot an error, when a date parameter is in a different format.

Doesn’t have to create new objects every time

This is the result of separation of instantiation and a place it is used. As a matter of fact it is the property of all creational patterns. The classic example is in java.lang.Boolean, which just returns constants:

Can return any subtype of return type

This benefit is clearly visible when using various libraries that leverage Service Provider Framework. We can program against an API and at run-time use different versions of libraries. This reduces conceptual weight of an API.

Example: To instantiate a Validator from Bean Validation we call Validation.buildDefaultValidatorFactory() static factory method. It will return registered implementation of ValidatorFactory, which can be either Hibernate Validator or Apache BVal:

The map may be static or not, but the point is that we create such mapping only once. The initialization is verbose and split into two parts, which prevents use of immutable objects. This can be easily improved with a static factory method:

Harder to distinguish from other static methods

In case when a class has many static methods, static factory methods may be harder to distinguish from others. In such case using consistent naming convention somewhat helps. Usually static factory methods use these prefixes: new, of, valueOf, from.

This is not always a problem, but may manifest itself especially in Utils classes so popular in enterprise level software. ;-)

They don’t scale to large number of optional parameters

Just like constructors, they don’t scale. When we need to pass many parameters just to create an object, then we should consider if the object is cohesive and if so consider using the Builder Pattern.

Make testing harder

Use Static Factory Methods (and any static method in general) when initialization is simple and doesn’t involve further collaborators or external resources. Always think: “How am I going to test it?“. External dependencies (database, files, etc.) make things much more complicated.