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:
- 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.
- Creation responsibility - The factory should not know how to create the objects.
- Ease of use.
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:
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.
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:
The derived classes look like this:
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
The static function
ObjectCreator keeps the responsibility of creating objects within the class.
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
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
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).
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)
AObject aobject = null;
for (int i = 0; i<100; i++)
aobject = ObjectFactory.CreateObject(i%3+1, null);
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.