Click here to Skip to main content
12,696,179 members (31,049 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked

Delegate Factory

, 11 Jan 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
A dynamic factory using delegates.


The target of this article is to show another factory implementation using delegates. I've searched CodeProject for a similar implementation and though some are close they're not quite the same. So I've decided to post this factory which I use in my current project.

This article does not aim at explaining the whole concept of factory or why you would use that particular design pattern.

Table of contents


The goal in writing this factory are as follows:

  1. Dynamic factory - It will be possible in the future to use the same factory for new types of objects without opening the factory's code.
  2. Creation responsibility - The factory should not know how to create the objects.
  3. Ease of use.

Using a delegate

Using delegates will help us achieve goals 1 and 2, it might set us on a collision course with goal 3 but we'll see.

The declaration of the delegate looks like this:

/// <span class="code-SummaryComment"><SUMMARY>

This delegate will help us obscure the way objects are created from the factory and help us register delegates of this type on the factory.

Implementing the object hierarchy

The factory will create our first usable object in our tree, so that we don't have to downcast straight away (only if it's really needed).

The base class is abstract and defines one variable member and one abstract function:

/// <span class="code-SummaryComment"><SUMMARY>

The derived classes look like this:

/// <span class="code-SummaryComment"><SUMMARY>

As you can see we implemented a Print function for the derived that does something (hopefully something different than other derived) and we've added a static function that creates a new object of Class1.

The static function ObjectCreator keeps the responsibility of creating objects within the class.

The factory

The factory class maps the classes' static functions, with the help of a delegate to the type that we wish to create.

The map is done through a hashtable with the type of the object being the key and the delegate that encapsulates a static function as the value.

The factory:

/// <span class="code-SummaryComment"><SUMMARY>

Notice that the factory has register and unregister functions, later on we'll see how to use them.

RegisterHandler - The register function that takes a delegate and inserts it to the hashtable with the class identifier as the key.

UnregisterHandler - The unregister function takes the type and removes whatever delegate that was there as the value.

CreateObject - The function that creates objects according to their type (key). This function extracts a delegate from the hashtable from position type and invokes the delegate (calls the static function of the object that we've registered before).

Using the factory

After comments from numerous esteemed colleagues, I changed the following code a bit, so it's more obvious that the factory doesn't "know" what type it's getting, but rather checks to see if it knows how to create an object with the provided key (type).

static void Main(string[] args)
        // registering the types that the factory will create
                new ObjectCreator(Class1.ObjectCreator));
                new ObjectCreator(Class2.ObjectCreator));
                new ObjectCreator(Class3.ObjectCreator));

        AObject aobject = null;
        // creating the objects
        for (int i = 0; i<100; i++)
                aobject = ObjectFactory.CreateObject(i%3+1, null);
        // unregistering a type
        if (!ObjectFactory.UnregisterHandler(Class1.ClassType))
            Console.WriteLine("Really ?!");

        // trying to create an unregistered type
        aobject = ObjectFactory.CreateObject(Class1.ClassType, null);
        if (aobject != null)
            Console.WriteLine("aobject is null");
    catch (Exception ex)

Notice the registration process:

We pass a type to be registered and we create a new delegate passing it the static function of the type that we're registering. We have to make sure that types are registered before we can use the factory to create objects, so the registration process should be as soon as possible in the program.

One more thing to notice is that Class3 (if you've downloaded the code, you may have noticed already) belongs to the same namespace (and assembly) as Main and not to the namespace (and assembly) that the rest of the classes belong to. This means that we have achieved goal 1 and goal 2.

I leave it up to you to decide if goal 3 was achieved.

Best regards and a happy new year to all.


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


About the Author

Asa Meltzer
Software Developer
Israel Israel
Software designer and programmer.
Programming languages:
MFC, C++, Java , C#, VB and sometimes C and assembly.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Ben_Desjardins11-Jan-10 7:44
memberBen_Desjardins11-Jan-10 7:44 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 11 Jan 2005
Article Copyright 2005 by Asa Meltzer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid