Design patterns: Abstract Factory

The Abstract Factory pattern is used to delegate the responsibility of creating object to another class. Therefore the factory is implemented as an object and provides an interface for creating families of related classes.

The creation of the class instances itself may be implemented by Factory Methods provided by these classes. The Abstract Factory hides the construction details. Furthermore you may create different Abstract factories implementing the same interface but create different class instances.

Therefore an Abstract Factory pattern is best utilized when your system has to create multiple families of classes or you want to provide a library of classes without exposing the implementation details. It allows future extension as it follows the “open closed principle”.

The following source code shows an according example. In our software system we have query and command classes to get or set data. Our data handling may be implemented by using a database or files. We offer these query and command classes independent of the type of data store. But of course they must match together. So we want to create different families of related objects. That’s the perfect use case for an Abstract Factory.

At first we define the interface for the factory. Our factory shall provide method to create the query and command classes.

interface IEnvironmentFactory
{
    IQuery GetQuery();
    ICommand GetCommand();
}

At next we can implement two different versions of the factory, one for each type of data store. The source code shows the factory relevant code only and does not include the code for the query and command interfaces and classes.

class DatabaseBasedEnvironment : IEnvironmentFactory
{
    public ICommand GetCommand()
    {
        return new DatabaseCommand();
    }

    public IQuery GetQuery()
    {
        return new DatabaseQuery();
    }
}

class FileBasedEnvironment : IEnvironmentFactory
{
    public ICommand GetCommand()
    {
        return new FileCommand();
    }

    public IQuery GetQuery()
    {
        return new FileQuery();
    }
}

This pattern is open for future extensions. For example if you want to add a new data store type, for example a cloud based data store, you can simply add a new factory and add it to you project. In this case there is no need to change the already existing implementations for the database and file based data handling.

Advertisements
Dieser Beitrag wurde unter C#, Clean Code, Design Pattern veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s