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+

Abstract Factory Design Pattern

The abstract factory pattern is a design pattern that allows for the creation of groups of related objects without the requirement of specifying the exact concrete classes that will be used. One of a number of factory classes generates the object sets.

Example Abstract Factory

To show a simple example of the abstract factory design pattern in action, we will create two factories that generate the packaging and delivery document information described earlier. One factory will be responsible for creating delivery objects for standard parcels. The second will generate objects for delicate parcels. To keep the examples small, no functionality will be included within the classes unless it relates directly to the design pattern. The only change to the core pattern is the addition of two properties in the Client class to provide public access to the two generated objects.

public class Client
{
    private Packaging _packaging;
    private DeliveryDocument _deliveryDocument;

    public Client(PurchaseFactory factory)
    {
        _packaging = factory.CreatePackaging();
        _deliveryDocument = factory.CreateDeliveryDocument();
    }

    public Packaging ClientPackaging
    {
        get { return _packaging; }
    }

    public DeliveryDocument ClientDocument
    {
        get { return _deliveryDocument; }
    }
}


public abstract class PurchaseFactory
{
    public abstract Packaging CreatePackaging();

    public abstract DeliveryDocument CreateDeliveryDocument();
}


public class StandardPurchaseFactory : PurchaseFactory
{
    public override Packaging CreatePackaging()
    {
        return new StandardPackaging();
    }

    public override DeliveryDocument CreateDeliveryDocument()
    {
        return new PostalLabel();
    }
}


public class DelicatePurchaseFactory : PurchaseFactory
{
    public override Packaging CreatePackaging()
    {
        return new ShockProofPackaging();
    }

    public override DeliveryDocument CreateDeliveryDocument()
    {
        return new CourierManifest();
    }
}

public abstract class Packaging { }

public class StandardPackaging : Packaging { }

public class ShockProofPackaging : Packaging { }

public abstract class DeliveryDocument { }

public class PostalLabel : DeliveryDocument { }

public class CourierManifest : DeliveryDocument { }

Testing the Abstract Factory

The example implementation of the abstract factory pattern can be tested by adding the above classes to a console application named "AbstractFactoryPattern" and by adding the following code to the Main method. This code creates two Client objects, each being passed a different type of factory in its constructor. By outputting the types of the generated objects, accessed through the Client's properties, you can see that the factories have created a different pair of object types in each case.

PurchaseFactory spf = new StandardPurchaseFactory();
Client standard = new Client(spf);
Console.WriteLine(standard.ClientPackaging.GetType());
Console.WriteLine(standard.ClientDocument.GetType());

PurchaseFactory dpf = new DelicatePurchaseFactory();
Client delicate = new Client(dpf);
Console.WriteLine(delicate.ClientPackaging.GetType());
Console.WriteLine(delicate.ClientDocument.GetType());

/* OUTPUT

AbstractFactoryPattern.StandardPackaging
AbstractFactoryPattern.PostalLabel
AbstractFactoryPattern.ShockProofPackaging
AbstractFactoryPattern.CourierManifest

*/
4 August 2008