Activator.CreateInstance vs Factory Pattern

Selçuk Öztürk picture Selçuk Öztürk · Apr 6, 2010 · Viewed 7.2k times · Source

What is the difference between Activator.CreateInstance and factory? Can they be used interchangeably? Or stil do we need a factory pattern?

Answer

KMån picture KMån · Apr 6, 2010

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