Click here to Skip to main content
Licence CPOL
First Posted 7 Feb 2006
Views 55,552
Bookmarked 39 times

Generic Object Factory

By | 7 Feb 2006 | Article
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:

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; 
        
        //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)

About the Author

Rob Hemstede

Team Leader

Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGeneric Object Factory With Constructor Arguments PinmemberJBjb17:31 19 Apr '07  
GeneralReflection Pinmemberrstaylor5:44 14 Feb '06  
GeneralRe: Reflection PinmemberRob Hemstede9:28 14 Feb '06  
GeneralRe: Reflection Pinmemberrstaylor10:08 14 Feb '06  
GeneralRe: Reflection Pinmemberzeleps23:52 14 Feb '06  
GeneralRe: Reflection PinmemberRob Hemstede3:12 15 Feb '06  
GeneralRe: Reflection Pinmemberzeleps8:23 15 Feb '06  
GeneralRe: Reflection Pinmemberchris32:55 15 Feb '06  
GeneralRe: Reflection PinmemberRob Hemstede9:57 15 Feb '06  
AnswerRe: Reflection PinmemberDanilo Corallo0:12 17 Jul '07  
Generalnot bad Pinmemberlxwde15:22 7 Feb '06  
GeneralRe: not bad Pinmembermatthew_evans20:36 7 Feb '06  
GeneralRe: not bad Pinmemberzeleps23:46 14 Feb '06  
GeneralErmmmm Pinmemberleppie10:33 7 Feb '06  
GeneralRe: Ermmmm PinmemberRob Hemstede9:02 8 Feb '06  
GeneralRe: Ermmmm Pinmemberqwerty666qwerty66612:36 14 Feb '06  
GeneralRe: Ermmmm Pinmemberchris32:38 15 Feb '06  
GeneralRe: Ermmmm Pinmemberqwerty666qwerty6664:44 15 Feb '06  
GeneralRe: Ermmmm Pinmemberchris35:14 15 Feb '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 7 Feb 2006
Article Copyright 2006 by Rob Hemstede
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid