Comparison
Factory methodvsSimple factory
Factory method
the base class does the work and calls a method for the object it needs, and each subclass answers that with a different class.
A method the base class calls to obtain a collaborator, overridden by subclasses to decide which concrete class arrives. It is a single decision point, delegated down the hierarchy — one product, one method — which is exactly what distinguishes it from abstract factory. In modern code the same effect is usually reached by injecting a function or a supplier rather than by subclassing, which is worth saying out loud in an interview.
Full entry →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 →