Design patterns: Factory Method

A factory method is a single method to create an object instance. The method is provided by the object itself. Therefore it can be overridden in a subclass.

By using the factory method pattern the object exposes a method to a client for creating the object. The factory method is provided by all derived classes of the object as well as other classes implementing the same factory method interface. Therefore this pattern is used in case where a client doesn’t know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job. It will make creation of concrete class easier by hiding details of constructor or initialization needs.

The factory method pattern can be implemented in several ways. Within the following example we will define an interface with a factory method and we implement a base class and some derived classes which will implement this interface. The creation of the object is done within the constructor which calls the factory method. It will contain all code needed to set up the instance. So a factory method can be part of the object creation process and must not explicitly return an object otself.

So let’s start with the interface definition. We want to create a query class to query some data and we define a factory method interface for this kind of query class.

interface IQuery
{
    string GetData();
}

interface IQueryFactory
{
    void CreateQuery();
}

At next we implement the base query class.

class Query : IQuery, IQueryFactory
{
    public Query()
    {
        CreateQuery();
    }

    public virtual void CreateQuery()
    {
        //implement object initialization logic here
    }

    public string GetData()
    {
        return "abc";
    }
}

An object instance can now be created by calling the factory method. To see the advantage of this factory method we will create some more types of query classes. For example we can create specific query classes for querying a file or a database. Furthermore we add a class for a specific database. As you can see we can use the factory method of the base class or we can override it with a specific one.

class FileQuery : Query
{
    public override void CreateQuery()
    {
        //implement object initialization logic here
    }
}

class DatabaseQuery : Query
{
}

class OracleDatabaseQuery : DatabaseQuery
{
    public override void CreateQuery()
    {
        //implement object initialization logic here
    }
}        

The factory method pattern will now offer the advantage that we can create an object instance just by calling the factory method. It will hide all implementation details, for example constructors with several parameters, and it internally execute all maybe necessary configuration or initialization procedures.

Advertisements
Dieser Beitrag wurde unter .NET, 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