Posts
20
Comments
10
Trackbacks
0
Women implement the Factory Method Pattern???
Advanced Technology
The other day while trawling the archive of the web comic xkcd (My thanks go out to singe for showing me the light. It is GOOD!) I happened upon the comic that is the inspiration for this post.

Now before I get started, I have to mention that a while ago I studied Design Patterns, but I brushed then off as silly nuisance. Back then they were nothing more that people writing about obvious facts that any/everyone knows and seemed more general knowledge. What I have recently noticed is that that is EXACTLY what they are. Design patterns common problems with solutions that can be used over and over again to solve them.

Back to the subject at hand: The web comic expresses a man’s (who must be a geek) unbelief and fascination of how a woman has a factory for creating more of herself. For those that have not noticed, this is along the lines of the Factory Method Pattern, and that it the subject of this post.

Factory Method Pattern

The factory method pattern allows classes to defer instantiation to their subclass.
In the basic form, the factory method pattern involves a creator and a product. The product is an abstract representation of a group of products while the creator is the base class of a possible creator hierarchy. A user asks the creator for an instance of a product and the creator will return it as a product. Rather confused? Not to worry? Remember, we are talking about the factory method pattern. What do factories do? They create stuff? Some pretty black pictures may help.
Basic Example

As we can see from the above the Creator creates instances of Product. The user does not need to worry about how the concreteproduct is created or what type of product it is since the concretecreator will return the concreteproduct via its interface. The words polymorphasism spring to mind. The creator similarly reliese on the concretecreators to create the correct concreteproject. Each conctretecreator is responsible for its own conctreteproduct.

As I said at the beginning this is only the vanilla flavor of the factor method patter. Derivatives allow for the creator to return a default instance of the product and more excitingly for the creator to be told what type of product it should create.
As I typed that last sentence I could see a big switch statement appear in my mind.

switch (productType)
{
case ConcreteProduct1:
return (Product) new ConcreteProduct1();
break;
case ConcreteProduct2:
return (Product) new ConcreteProduct2();
break;
...

But this is silly code that we would have written back in 2002 or before. Thanks to .Net (and other frameworks :)) we have funky things like Generic Types and Reflection. Using these we can do some rather funky things like create and instance of a class from a passed class type and return it as an interface that passed class implements.

public static class Creator
{
public static I Create<I,C>()
where I : class
where C : class,I
{
return (I) Activator.CreateInstance(typeof (C));
}
}

More Enhanced Example

What we now have is a single creator class that can create an instance of any class and return it as an interface that that class implements. I have to admit that this code is rather staggering (WOW) for me (says something about what I have been writing in the past). I have to credit DarkSide for its creation.

Hopefully this has helped you build an idea of what factory classes are all about. If not, or you think that I may have missed something let me know.

Oh... if you are interested take a look at this (FactoryPattern.zip) code example as a supliment for the above discussion.
posted on Thursday, March 13, 2008 7:01 PM Print
News