The purpose of the Factory Method Design Pattern is to create objects without the general factory class having to know the exact class names. In this way a general factory can be used to create concrete factories which in turn create specific products.
The Factory Method pattern separates the creator classes from the products they are designed to generate and thus encapsulate the creation of objects (products) which can be useful if the creation process is complex like when it depends on user input or settings in a configuration file, etc.
For example, I will create a general class that has a single static method which will be the factory method for this example:
Take special note to how the factory method returns a
new object which is specified by the
In the next example the factory method will be defined as public and called directly by the client code. I will use interface implementations instead of subclasses. Either way is an acceptable technique in order to achieve the pattern: