BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Factory Method Design Pattern

The factory method pattern is a design pattern that allows for the creation of objects without specifying the type of object that is to be created in code. A factory class contains a method that allows determination of the created type at run-time.

What is the Factory Method Pattern?

The factory pattern is a Gang of Four design pattern. This is a creational pattern as it is used to control class instantiation. The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.

A good example of the use of the factory pattern is when creating a connection to a data source if the type of the data source will be selected by the end-user using a graphical interface. In this case, an abstract class named "DataSource" may be created that defines the base functionality of all data sources. Many concrete subclasses may be created, perhaps "SqlDataSource", "XmlDataSource", "CsvDataSource", etc, each with specific functionality for a different type of data. At run-time, a further class, perhaps named "DataSourceFactory", generates objects of the correct concrete class based upon a parameter passed to the factory method.

Implementing the Factory Method Pattern

Factory Method Design Pattern UML

The UML class diagram above describes an implementation of the factory method design pattern. In this diagram there are four classes:

  • FactoryBase. This is an abstract base class for the concrete factory classes that will actually generate new objects. This class could be a simple interface containing the signature for the factory method. However, generally an abstract class will be used so that other standard functionality can be included and inherited by subclasses. In simple situations the factory method may be implemented in full here, rather than being declared as abstract.
  • ConcreteFactory. Inheriting from the FactoryBase class, the concrete factory classes inherit the actual factory method. This is overridden with the object generation code unless already implemented in full in the base class.
  • ProductBase. This abstract class is the base class for the types of object that the factory can create. It is also the return type for the factory method. Again, this can be a simple interface if no general functionality is to be inherited by its subclasses.
  • ConcreteProduct. Multiple subclasses of the Product class are defined, each containing specific functionality. Objects of these classes are generated by the factory method.

The following code shows the basic code of the factory method design pattern implemented using C#:

public abstract class FactoryBase
{
    public abstract ProductBase FactoryMethod(int type);
}


public class ConcreteFactory : FactoryBase
{
    public override ProductBase FactoryMethod(int type)
    {
        switch (type)
        {
            case 1: return new ConcreteProduct1();
            case 2: return new ConcreteProduct2();
            default: throw new ArgumentException("Invalid type.", "type");
        }
    }
}


public abstract class ProductBase { }

public class ConcreteProduct1 : ProductBase { }

public class ConcreteProduct2 : ProductBase { }

In this skeleton structure you can see five distinct classes. The first of these is the FactoryBase class. This is the base class for all concrete factories. A single abstract method is declared, accepting a parameter specifying the type of object to create and returning a ProductBase instance.

The ConcreteFactory class is derived from FactoryBase. This class is used to generate product objects. You can see that the FactoryMethod method overrides the base class version. It accepts an integer parameter and uses this in a switch statement to decide which type of product to instantiate and return. If the parameter is invalid, an exception is thrown.

The ProductBase class is the base class for all classes that can be created by the factory. When a new object is instantiated by the factory it is returned in a ProductBase object.

Finally we have two subclasses of ProductBase. These are the concrete products that can be created by the factory and would contain the specialised functionality required for each type.

11 July 2008