#821 – The Factory Pattern

Factory is a design pattern that you can use when you don’t want client code creating objects directly, but you want to centralize object creation within another class. To get a new instance of an object, you call a method in the factory, which creates the object on your behalf.

In the example below, DogFactory.CreateDog is a factory method, which does the actual creation of a Dog object.

There are sometimes advantages that argue for a singleton over just doing static methods, e.g. being able to treat the factory as an actual object. See http://www.dotnetperls.com/singleton-static for a nice comparison.