jargon

Comparison

Simple factoryvsStatic factory method

Simple factory

one function takes a string and returns the right implementation, and everyone calls it instead of choosing themselves.

A single place — usually one function with a switch — that maps some input to a concrete class, so the mapping is stated once. It is not in the Gang of Four catalogue and people are sometimes told off for calling it a pattern, but it is by far the most common thing actually written when someone says 'factory'. Reach for it first; the polymorphic factory method is what you graduate to when the switch itself needs to vary.

Full entry →

Static factory method

the class has private constructors and a set of named methods that build it, so the call site says what kind you wanted.

A static method on a class that returns an instance of it, in place of a public constructor. It buys three things a constructor cannot: a name, so two constructions with the same parameter types are distinguishable; freedom to return a cached or subclass instance; and a place to validate before anything exists. The cost is that a class with no public constructor cannot be subclassed conventionally, which is often the intent anyway.

Full entry →

Related comparisons