The factory pattern is a creational pattern that provides a template that can be used to create objects. It is used in complex situations where the type of object required varies and needs to be specified in each case. It does not use the new keyword directly to instantiate objects. This means it does not explicitly require the use of a constructor to create objects. Instead, it provides a generic interface that delegates the object creation responsibility to the corresponding subclass.
In plain words: the factory simply generates an instance for the client without exposing any instantiation logic to the client
As the name “factory” implies, we can use this pattern when we want to create different objects that have similar characteristics
when to use
When the type of objects required cannot be anticipated beforehand
When multiple objects that share similar characteristics need to be created
When you want to generalize the object instantiation process since the object set-up is complex in nature