Let's define a couple of terms for clarity:
- A virtual method can be overridden by a subclass.
- An abstract method has no implementation. It needs to be overridden or implemented in a subclass.
(Language-specific details: In C++, an abstract virtual method is called “pure virtual”. In Java, all methods are virtual unless declared final.)
The factory method pattern requires that the factory method can be overridden or implemented in a subclass, i.e. that this method is virtual. It does not require that the base class declares this method as abstract, i.e. the base class may provide an implementation of that method.
- If the factory method is abstract in the base class, the subclass must implement it.
- If the factory method is implemented in a base class, the subclass may optionally override it. The base class provides a default implementation.
In the case where implementing the method is optional, this does not detract from the pattern. The important aspect of the pattern is not that there is a subclass that decides which class to instantiate, but only that it is possible for a subclass to decide.
The Design Patterns book which introduced this pattern mentions the abstract/non-abstract variants explicitly:
The two main variations of the Factory Method pattern are (1) the case when the Creator class is an abstract class and does not provide an implementation for the factory method it declares, and (2) the case when the Creator is a concrete class and provides a default implementation for the factory method. It's also possible to have an abstract class that defines a default implementation, but this is less common.
The first case requires subclasses to define an implementation[.] In the second case, the concrete Creator uses the factory method primarily for flexibility.