Click here to Skip to main content
16,018,664 members
Articles / Programming Languages / C#
Article

Generic Object Factory

Rate me:
Please Sign up or sign in to vote.
3.87/5 (17 votes)
7 Feb 2006CPOL1 min read 95.1K   42   19
A generic object factory using C#.

Introduction

An object factory mechanism is needed whenever polymorphic objects need to be created whose concrete type can only be determined at runtime. The most straightforward implementation of an object factory would be something like:

C#
Fruit Create( string strType )
{   
    switch( strType )
    {
        case "Apple":
            return new Apple();
        case "Orange":
            return new Orange();
    }    
}

But this implementation has a drawback. Whenever you add a new object type, the factory has to be updated. In C++, there are some well known generic solutions to this problem. For more information about this, I refer to Andrei Alexandrescu – Modern C++ Design. Now that C# also supports generics, I wondered if a similar solution would be possible in this language.

The code

The heart of the factory is the SortedList that holds a delegate for each registered object. The Register function creates these delegates by instantiating a generic member function for the object that is supplied as the type argument. To create a registered object, the Create function just looks up the appropriate delegate and executes it.

C#
using System.Collections.Generic;

public struct Factory < KeyType, GeneralProduct > 
{
    //Register a object with the factory
    public void> Register< SpecificProduct >(KeyType key)
        where SpecificProduct : GeneralProduct, new()
    {
        if( m_mapProducts == null )
                {
            m_mapProducts = new SortedList< KeyType, CreateFunctor >(); 
        }
        CreateFunctor createFunctor = Creator<SpecificProduct>; 
        m_mapProducts.Add(key, createFunctor);
    }

    //Create a registered object 
    public GeneralProduct Create( KeyType key )
    {
        CreateFunctor createFunctor = m_mapProducts[ key ];
        return createFunctor(); 
    }
    
    private GeneralProduct Creator < SpecificProduct >() 
        where SpecificProduct : GeneralProduct, new()
    {
        return new SpecificProduct();
    }

    private delegate GeneralProduct CreateFunctor(); 

    private SortedList<KeyType, CreateFunctor>  m_mapProducts;
}

Using the code

Here is an example on how to use the generic object factory:

C#
class Fruit
{ 
} 

class Apple : Fruit 
{ 
} 

class Orange : Fruit 
{ 
} 

class TestClass
{ 
    static void Main(string[] args) 
    { 
        General.Factory< string, Fruit > factory; 
        
        //Register
        factory.Register< Apple >( "Apple" ); 
        factory.Register< Orange>( "Orange" ); 
        
        //Create 
        Fruit fruit1 = factory.Create("Apple"); 
        Fruit fruit2 = factory.Create("Orange"); 
    } 
}

The example shows two objects, both derived from the same Fruit base class. Both the base class and the identifier type need to be supplied as type arguments when instantiating the factory. In this example, I use a string as the identifier for the registering objects, in general an enum type would be more appropriate.

History

  • Initial version - 2/12/2006.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGeneric Object Factory With Constructor Arguments Pin
JBjb19-Apr-07 17:31
JBjb19-Apr-07 17:31 
GeneralReflection Pin
rstaylor14-Feb-06 5:44
rstaylor14-Feb-06 5:44 
GeneralRe: Reflection Pin
Rob Hemstede14-Feb-06 9:28
Rob Hemstede14-Feb-06 9:28 
GeneralRe: Reflection Pin
rstaylor14-Feb-06 10:08
rstaylor14-Feb-06 10:08 
GeneralRe: Reflection Pin
zeleps14-Feb-06 23:52
zeleps14-Feb-06 23:52 
GeneralRe: Reflection Pin
Rob Hemstede15-Feb-06 3:12
Rob Hemstede15-Feb-06 3:12 
GeneralRe: Reflection Pin
zeleps15-Feb-06 8:23
zeleps15-Feb-06 8:23 
GeneralRe: Reflection Pin
chris315-Feb-06 2:55
chris315-Feb-06 2:55 
GeneralRe: Reflection Pin
Rob Hemstede15-Feb-06 9:57
Rob Hemstede15-Feb-06 9:57 
AnswerRe: Reflection Pin
Danilo Corallo17-Jul-07 0:12
Danilo Corallo17-Jul-07 0:12 
Generalnot bad Pin
lxwde7-Feb-06 15:22
lxwde7-Feb-06 15:22 
Got my five, however, as leppie said, it's more convenient to use reflection to do this in C#.D'Oh! | :doh:






GeneralRe: not bad Pin
matthew_evans7-Feb-06 20:36
matthew_evans7-Feb-06 20:36 
GeneralRe: not bad Pin
zeleps14-Feb-06 23:46
zeleps14-Feb-06 23:46 
GeneralErmmmm Pin
leppie7-Feb-06 10:33
leppie7-Feb-06 10:33 
GeneralRe: Ermmmm Pin
Rob Hemstede8-Feb-06 9:02
Rob Hemstede8-Feb-06 9:02 
GeneralRe: Ermmmm Pin
qwerty666@codeproject.com14-Feb-06 12:36
qwerty666@codeproject.com14-Feb-06 12:36 
GeneralRe: Ermmmm Pin
chris315-Feb-06 2:38
chris315-Feb-06 2:38 
GeneralRe: Ermmmm Pin
qwerty666@codeproject.com15-Feb-06 4:44
qwerty666@codeproject.com15-Feb-06 4:44 
GeneralRe: Ermmmm Pin
chris315-Feb-06 5:14
chris315-Feb-06 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.