Factories are implemented using the factory method pattern. The intent of this pattern is to allow the creation of objects without specifying their classes. This is accomplished by invoking a factory method. The main goal of a factory method is to create an instance of a class.
You use the factory method pattern for the following scenarios:
- When the class is unable to anticipate the type of object that must be instantiated
- When the subclass must specify the type of object to instantiate
- When the class controls the instantiation of its objects
Consider the following diagram:
As you can see from the preceding diagram, you have the following items:
- Factory, which provides the interface for the FactoryMethod() that returns a type
- ConcreteFactory, which overrides or implements the FactoryMethod() to return a concrete type
- ConcreteObject...