Comparison
Domain factoryvsFactory method
Domain factory
creating a valid aggregate needed four objects and a rule, so a separate object does it and the constructor stayed private.
An object or method whose job is to construct a complex aggregate correctly, when doing so requires knowledge that does not belong in the constructor. It is the domain-driven design use of the word, which is narrower than the creational patterns of the same name: the intent is protecting an invariant during assembly, not choosing among implementations. It disappears again as soon as construction is simple enough for a static factory method to carry.
Full entry →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 →