The Factory Method is similar to SimpleFactory
, but it is a little bit more complicated. As shown in the following diagram, typically this design pattern has an abstract class, Creator
, that contains the factory_method
which is responsible for creating some kind of objects. The some_operation
method then works with the created object. The ConcreteCreator
class can redefine the factory_method
to change the created object in the runtime. The some_operation
method does not care which object is created as long as it implements the Product
interface and provides the implementation for all methods in that interface.
The essence of this pattern is to define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The interface is factory_method
in the Creator
and ConcreteCreator
classes, which decides which subclass of Product
to create. The Factory Method is based on inheritance; object creation is delegated to the...