Yes, you are right.
The pattern is called like that because it describes a known, well accepted solution using a factory method.
The pattern could have been named "XXXX". That doesn't matter.
Regarding @pdr's comment about what you describe being really an "Abstract Factory", the following images clarify the difference, Factory Method is exactly what you describe:
The images are taken from this PDF taken from this site. It was maded by a software engineer by the name of Jason McDonald.
A factory method is essentially just a constructor with another name. This can be useful e.g. to guarantee correct initialization, registration, or to supply default values without having to subclass the product:
private List<WeakReference<Product>> products = ...;
public Product makeProduct(Param x) {
Product instance = new Product(x, "default value");
instance.initialize(42);
products.add(new WeakReference<>(instance));
return instance;
}
An abstract factory pattern is usually implemented in terms of factory methods, but here the aim is to leave the concrete type unspecified until runtime. So we have a set of products:
interface Product {}
class ConcreteProduct1 implements Product {}
class ConcreteProduct2 implements Product {}
And we have an abstract factory:
interface Factory {
public Product makeProduct();
}
Such a factory might be used by client code: factory.makeProduct()
. Which concrete product is used is delayed to runtime.
class ConcreteFactory1 implements Factory {
public Product makeProduct() { return new ConcreteProduct1(); }
}
class ConcreteFactory2 implements Factory {
public Product makeProduct() { return new ConcreteProduct2(); }
}
Typically, an abstract factory will have multiple factory methods for a set of related types, you can think of a factory instance as a “theme”. Abstract factories are also useful for dependency injection.
The factory method pattern is a combination of factory methods with the strategy pattern. Here the idea is that our class needs to create some instance, but will let subclasses override the choice of the instance. In other words, the client and the factory are the same.
class DomainSpecificStuff {
public doDomainSpecificStuff() {
Product p = this.makeProduct();
p.frobnicate();
}
protected makeProduct() {
return new ConcreteProduct1();
}
}
class OtherDomainSpecificStuff {
@Override
protected makeProduct() {
return new ConcreteProduct2();
}
}
So the strategy pattern allows for limited dependency injection through the use of inheritance when used as the FMP.
So what are the useful distinctions between the factory method pattern and abstract factory pattern?
Client: The client of the factory method in the FMP is the factory itself – it's just a spin on the strategy pattern, after all. The client of the AFP is some other class.
Number of Objects: Typically, the FMP will only have a single factory method, whereas the AFP can be used to produce multiple related objects. It is not the case that an AFP must have at least two factory methods, although the creation of related, interdependent objects is where the AFP shines.
Intent: The AFP is a collection of related factory methods which can be passed around in client code. The FMP allows subclasses to swap out a dependency.
Best Answer
According to the Open/Close Principe (OCP):
Let's examine your static factory architecture under this perspective:
ProductThree
. How could themakeProduct()
return an object that could be one of the subclass without knowing the constructor of the subclasses ? I can think of two approaches:switch
or chainedif
. This would infringe OCP.Product
from its subclasses (e.g. using a map / associative container mapping a class name / identifier with a class maker method- Java example). This would meet extensibility requirement and remain compliant with OCP.ProductOne
intoProductOneA
andProductOneB
. To achieve this, you could not rely on sub class constructors inmakeProduct()
. You would have to use amakeProductOne()
. This requires that the self registration is designed so to enable cascading through the hierachy. Although this could be challenging, it doesn't seem impossible.In conclusion, the concept of the static factory is compliant with OCP. Of course, specific implementation might infringe OCP, but only if they wouldn't take into consideration the additional requirements I mentioned above.
Note that using a factory class is a cleaner approach : it enforces separation of concerns (i.e. avoid that the factory knows about internals of the Product). But with regard to OCP, using a static method as explained above is an acceptable alternative.
I can't tell for other languages, but Andrei Alexandrescu demonstrated the implementation of a factory method in "Modern C++ design - Generic Programming and design patterns applied". It's full OCP using a map and registering/unregistering functions. The only difficulty he mentions it the type identifier that has to be provided to the factory. Of course, an
enum
would be an OCP issue, but he explored several alternatives using RTTI or a unique ID generator to circumvent this issue.