What is the difference between Activator.CreateInstance
and factory? Can they be used interchangeably? Or stil do we need a factory pattern?
Activator.CreateInstance
is a static method that creates an instance of the specified type using that type's default constructor.
While factory pattern deals with the problem of creating objects (products) without specifying the exact class of object that will be created.
You can "use" the Activator.CreateInstance
within a factory pattern to return different types of objects. See this example. You can also create a generic factory utilizing Activator.CreateInstance