Click here to Skip to main content
13,894,325 members
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 7 Feb 2006
Licenced CPOL

Generic Object Factory

, 7 Feb 2006
Rate this:
Please Sign up or sign in to vote.
A generic object factory using C#.


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:

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.

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:

class Fruit

class Apple : Fruit 

class Orange : Fruit 

class TestClass
    static void Main(string[] args) 
        General.Factory< string, Fruit > factory; 
        factory.Register< Apple >( "Apple" ); 
        factory.Register< Orange>( "Orange" ); 
        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.


  • Initial version - 2/12/2006.


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


About the Author

Rob Hemstede
Team Leader
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGeneric Object Factory With Constructor Arguments Pin
JBjb19-Apr-07 17:31
memberJBjb19-Apr-07 17:31 
GeneralReflection Pin
rstaylor14-Feb-06 5:44
memberrstaylor14-Feb-06 5:44 
GeneralRe: Reflection Pin
Rob Hemstede14-Feb-06 9:28
memberRob Hemstede14-Feb-06 9:28 
GeneralRe: Reflection Pin
rstaylor14-Feb-06 10:08
memberrstaylor14-Feb-06 10:08 
GeneralRe: Reflection Pin
zeleps14-Feb-06 23:52
memberzeleps14-Feb-06 23:52 
GeneralRe: Reflection Pin
Rob Hemstede15-Feb-06 3:12
memberRob Hemstede15-Feb-06 3:12 
GeneralRe: Reflection Pin
zeleps15-Feb-06 8:23
memberzeleps15-Feb-06 8:23 
GeneralRe: Reflection Pin
chris315-Feb-06 2:55
memberchris315-Feb-06 2:55 
GeneralRe: Reflection Pin
Rob Hemstede15-Feb-06 9:57
memberRob Hemstede15-Feb-06 9:57 
AnswerRe: Reflection Pin
Danilo Corallo17-Jul-07 0:12
memberDanilo Corallo17-Jul-07 0:12 
Generalnot bad Pin
lxwde7-Feb-06 15:22
memberlxwde7-Feb-06 15:22 
GeneralRe: not bad Pin
matthew_evans7-Feb-06 20:36
membermatthew_evans7-Feb-06 20:36 
GeneralRe: not bad Pin
zeleps14-Feb-06 23:46
memberzeleps14-Feb-06 23:46 
GeneralErmmmm Pin
leppie7-Feb-06 10:33
memberleppie7-Feb-06 10:33 
GeneralRe: Ermmmm Pin
Rob Hemstede8-Feb-06 9:02
memberRob Hemstede8-Feb-06 9:02 
GeneralRe: Ermmmm Pin
qwerty666@codeproject.com14-Feb-06 12:36
memberqwerty666@codeproject.com14-Feb-06 12:36 
GeneralRe: Ermmmm Pin
chris315-Feb-06 2:38
memberchris315-Feb-06 2:38 
GeneralRe: Ermmmm Pin
qwerty666@codeproject.com15-Feb-06 4:44
memberqwerty666@codeproject.com15-Feb-06 4:44 
GeneralRe: Ermmmm Pin
chris315-Feb-06 5:14
memberchris315-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 7 Feb 2006
Article Copyright 2006 by Rob Hemstede
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid