Click here to Skip to main content
Click here to Skip to main content

Generic Object Factory

, 7 Feb 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Rob Hemstede
Team Leader
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
GeneralErmmmm Pinmemberleppie7-Feb-06 11:33 
GeneralRe: Ermmmm PinmemberRob Hemstede8-Feb-06 10:02 
GeneralRe: Ermmmm Pinmemberqwerty666qwerty66614-Feb-06 13:36 
GeneralRe: Ermmmm Pinmemberchris315-Feb-06 3:38 
GeneralRe: Ermmmm Pinmemberqwerty666qwerty66615-Feb-06 5:44 
GeneralRe: Ermmmm Pinmemberchris315-Feb-06 6:14 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 7 Feb 2006
Article Copyright 2006 by Rob Hemstede
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid