Simple factory pattern based on interfaces

The main purpose of factory design patterns is to separate object creation process from current implementation context. During coding you use factory to create objects. Based on incoming parameters factory decides which object to create. You don't know exact type of the object. Usually you know either base abstract class type that created object was inherited from or interface that created object is implementing. Based on this knowledge you can use object (call abstract class methods or interface methods). I found a lot of factories examples with abstract class connection, but none with interface connection. However interface connection is most common in commercial factory design pattern implementations. Many developers use Unity Framework or Castle Project (example of connection based on interface). Below I'll show how to implement factory with interface connection like it is done in Unity Framework or Castle Project.

Lets build animal fabric, which would create dogs and cats objects. Let's define ICat and IDog interfaces.

public interface ICat
{        
    string Meow();
}

public interface IDog
{
    string Bark();
    string Sit();
}

Now, lets define some Dog and Cat classes that implement ICat and IDog interfaces respectevly.

public class Cat:ICat
{               
    public string Meow()
    {
        return "Meow meow meow ...";
    }     
}

public class Dog: IDog
{        
 
    public string Bark()
    {
        return "Woof woof woof ...";
    }
 
    public string Sit()
    {
        return "I am sitting.";
    }
 
}

Now lets create factory:

public class DefaultFactory
{
    public static T Create<T>()
    {
        if (typeof(T) == typeof(IDog))
        {
            return (T)(IDog)new Dog();
        }
        else
            if (typeof(T) == typeof(ICat))
            {
                return (T)(ICat)new Cat();
            }
            else
            {
                throw new NotImplementedException(String.Format("Creation of {0} interface is not supported yet.", typeof(T)));
            }
    }
}

And here how you can use it:

IDog dog = DefaultFactory.Create<IDog>();
ICat cat = DefaultFactory.Create<ICat>();
Console.WriteLine(dog.Bark());
Console.WriteLine(dog.Sit());
Console.WriteLine(cat.Meow());


Output:

Woof woof woof ...
I am sitting.
Meow meow meow ...

Source code for sample project above.


Posted on Monday, September 5, 2011 by | Comments (3) | Add Comment

Comments

Gravatar

Re:Simple factory pattern based on interfaces

And I was just wondernig about that too!

Posted on 11/23/2011 7:53:25 AM by Jaclyn #
Gravatar

Re: Simple factory pattern based on interfaces

Nice! Don't forget to care about the visibility of types. Therefore the classes Dog and Cat can be (should be) marked as internal.

Posted on 2/14/2012 12:53:29 AM by Baba #
Gravatar

Yes, you are right

Gravatar

Re: Simple factory pattern based on interfaces

Concise and direct, no beating around the bush. Nice job!

Posted on 2/7/2014 9:56:03 AM by VCCGeek #

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5