The Factory Design Pattern is a Creational Pattern. The official definition for the Factory pattern is:

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

This pattern is used to replace a class constructor(s), allowing the type of object to be instantiated to be determined at run-time as opposed to design-time, and is used to control a class’s instantiation. The use of this design pattern reduces the coupling between classes, and offers much more flexibility in the future if the requirements of your application changes, which often happens. One tangible benefit of the Factory pattern is it allows the client to focus on its role in the architecture because it allows for separation the creation & instantiation of objects from the client.

Implementation:

To implement the Factory design pattern we will create an Interface which will define the classes that will be instantiated by our factory class. Here we're using a very simple example but know this is a very powerful design pattern and can be expanded to more complex structures & objects. So, for this we will have an interface (IEmployee) that has a single value, Salary that all our objects will implement (keep in mind this is just an example to show how this design pattern works and can be implemented):

Now we have 3 classes (factory objects) to demonstrate different employee types. We have Manager, Programmer, and DBA which all implement our IEmployee interface, utilizing a single read-only property:

Now we need a factory class to handle the instantiation of our factory objects. Although in our example our factory manager class only has a single method (factory method), this method completely encapsulates the creation of our factory objects. Our factory method is declared as static so no instance our the factory manager need be made:

Ok we now have our factory defined and created, so how do I use it you may ask, well I'll show you. We will generate a new employee for each class in a loop using the factory manager class, then print out each salary. This is done without the client ever knowing what object type is being instantiated, and this is because our factory class handles the instantiation of any employee type that is created:

static void Main(string[] args){ Dictionary<EmployeeFactory.EmployeeType, IEmployee> employees = new Dictionary<EmployeeFactory.EmployeeType, IEmployee>(); //let's create a new employee for each available employee type. Each item is insatantiated //without the client needing to know what type of object is being created (this is handled by the factory method) foreach (EmployeeFactory.EmployeeType e in Enum.GetValues(typeof(EmployeeFactory.EmployeeType))) { employees.Add(e, EmployeeFactory.GetEmployeeSalary(e)); } //now let's iterate through our dictionary and diplay the base salary for each factory object foreach (EmployeeFactory.EmployeeType type in employees.Keys) Console.WriteLine(string.Format("The base salary for {0} is {1:C}", type, employees[type].Salary)); Console.ReadKey();}

Well that is how the Factory Design Pattern works and is used, as you can see if can be extremely powerful and helpful (especially when dealing with complex creation processes, such as when the creation depends on input from the user or from some sort of configuration file) when designing complex software. Thanks for reading and hope you found it useful & informative.