Use parameterized factories when selection logic is simple enough for one creation component and does not need a full class hierarchy.
Parameterized factory: A factory that chooses which product to create based on an input parameter instead of an overridable creator method.
Many Java codebases do not need a full Factory Method hierarchy. They need one central place that maps a mode, type, enum, or configuration value to a concrete implementation. That is where parameterized factories can be the better trade.
A parameterized factory is usually a stronger choice when:
1enum ExportFormat {
2 PDF,
3 HTML
4}
5
6final class DocumentFactory {
7 Document create(ExportFormat format) {
8 return switch (format) {
9 case PDF -> new PdfDocument();
10 case HTML -> new HtmlDocument();
11 };
12 }
13}
This is still disciplined creation. It just avoids subclassing when subclassing is not buying anything.
Parameterized factories weaken when:
At that point, a more explicit factory-method or strategy-based design may age better.
The input should represent a real selection concern. Good inputs are things like:
Bad inputs are vague boolean flags or overloaded parameter sets that hide multiple different creation axes.
When reviewing a parameterized factory, ask:
Parameterized factories are useful because they are often the smallest pattern that works. The mistake is keeping them after the variation space has outgrown them.